How do you create a dropdown list dynamically using jQuery? By dropdown list, I mean a <select>
with its associated <option>
values.
When the Add button is clicked, the AddDropDownList JavaScript function is called. Inside the JavaScript function, first a JSON Array is generated. Then a dynamic DropDownList element is created and then using loop all items from the JSON array are added to the dynamic DropDownList element.
The <select> tab is used with <option> tab to create the simple dropdown list in HTML. After that JavaScript helps to perform operation with this list. Other than this, you can use the container tab <div> to create the dropdown list. Add the dropdown items and links inside it.
Just create the elements like any element.
Example:
var data = { 'foo': 'bar', 'foo2': 'baz' } var s = $('<select />'); for(var val in data) { $('<option />', {value: val, text: data[val]}).appendTo(s); } s.appendTo('body'); // or wherever it should be
In its simplest form,
var opt = "<option> -- Select -- </option>"; $(opt).wrap('<select />'); $('#some-container-div').html(opt);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With