Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

append option to select menu?

Using Javascript how would I append an option to a HTML select menu?

e.g to this:

<select>     <option value="volvo">Volvo</option>     <option value="saab">Saab</option>     <option value="mercedes">Mercedes</option>     <option value="audi">Audi</option> </select> 

http://jsfiddle.net/SSwhr/

like image 855
Skizit Avatar asked Mar 03 '11 15:03

Skizit


People also ask

How do you append to a select option?

Method 1: Append the option tag to the select box The option to be added is created like a normal HTML string. The select box is selected with the jQuery selector and this option is added with the append() method. The append() method inserts the specified content as the last child of the jQuery collection.

How to add options to dropdown using jQuery?

Add options to a drop-down list using jQuery. JavaScript Code: var myOptions = { val1 : 'Blue', val2 : 'Orange' }; var mySelect = $('#myColors'); $. each(myOptions, function(val, text) { mySelect.

How do I add dynamic options to select?

To dynamically add options to an existing select in JavaScript, we can use a new Option object append it to the select element with the options. add method. to add a select element. to select the select element with document.


1 Answers

Something like this:

var option = document.createElement("option"); option.text = "Text"; option.value = "myvalue"; var select = document.getElementById("id-to-my-select-box"); select.appendChild(option); 
like image 93
Marcus Frödin Avatar answered Sep 28 '22 05:09

Marcus Frödin