Skip to main content

Adding another option to Dropdown box using JavaScript

Below are the steps for adding an option to the dropdown box using JavaScript.
·  Create a Option element using JavaScript

      var opt = document.createElement("option");
·   Add Option object to dropdown box     

      document.getElementById("test").options.add(opt);

·   Assign value and text to the Option object just added

    opt.text = "New";
    opt.value = "0";
   
·    Insert this Option object just created at top position inside dropdown. This step is optional, if

this step is not performed the Option object will added to dropdown as the last item.

          document.getElementById("test").insertBefore(opt, document.getElementById("test").firstChild);

 ·    To make the Option object as default when the page loads put this code at the end and include it inside the function and call that function on page load.
          document.getElementById("test").options[0].selected=true;
Complete Code:

    function addItem()
      {
          var opt = document.createElement("option");
          document.getElementById("test").options.add(opt);
          opt.text = "New";
          opt.value = "0";
          document.getElementById("test").insertBefore(opt, document.getElementById("test").firstChild);
          document.getElementById("test").options[0].selected=true;
      }

Comments

Popular posts from this blog

How to convert SharePoint Data View Web Part in dropdown format and display unique items or values.

These steps will guide you through the process of coverting the SharePoint Data View Web Part into dropdown format and display the unique items or values from the list. Now to perform the steps below, the SharePoint designer and XSL knowlegde is required.     Start by adding a blank Data View Web Part on a page. Then attach the list source to the data view from data source library. For this example I will attach the test list that I created for this tutorial.After providing the source for the Data View all the columns from the list will be displayed. Now lets drag and drop one of the columns from the  data source on a Data View. At this point it will bring all the data from that column and display it in a table format. If it is previewed it will look like this :   Data View Web Part in Table Format  Now, lets convert this Data View Web Part into a drop down format. In a SharePoint designer click on the little arrow or bracket at t...

Returning selected value from Dropdown Box using JavaScript

Below is the example for returning value of the selected item in a Dropdown box: HTML Dropdown control or element with the id “test”              <select id="test" onchange="getVal();">                   <option value="1">1</option>                   <option value="2">2</option>                   <option value="3">3</option>            </select> JavaScript function for getting the selected value from dropdown: ·   First get the selected index from the dropdown and store it in a variable.        var ddIndex = document.getElementById("test...