Javascript - Add Options To Select From Input Text

How To Insert A New Option To DropDown List From TextBox Using Javascript

add item to drpdown list using javascript


In This Javascript Tutorial we will See How To Add A Option To Select From Input Text Value On Button Onclick Event Using JS And Netbeans Editor .


Project Source Code:

<!DOCTYPE html>
<html>
    <head>
        <title>Add New Option</title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
      
        <select id="select">
            <option value="java">Java</option>
            <option value="c#">C#</option>
        </select>
        <input type="text" id="val">
        <button onclick="insertValue();">Add</button>

        <script>
            
            function insertValue()
            {
                var select = document.getElementById("select"),
                    txtVal = document.getElementById("val").value,
                    newOption = document.createElement("OPTION"),
                    newOptionVal = document.createTextNode(txtVal);
             
                newOption.appendChild(newOptionVal);
                select.insertBefore(newOption,select.firstChild);
            }
            
        </script>

    </body>
</html>


OUTPUT:

insert options to dropdown list




Share this

Related Posts

Previous
Next Post »