Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create dropdown list dynamically using jQuery?

Tags:

jquery

How do you create a dropdown list dynamically using jQuery? By dropdown list, I mean a <select> with its associated <option> values.

like image 887
user550881 Avatar asked Jan 27 '11 09:01

user550881


People also ask

How to Add dropdown in HTML table cell dynamically?

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.

How to create drop-down list using JavaScript?

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.


2 Answers

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 
like image 76
Felix Kling Avatar answered Nov 01 '22 20:11

Felix Kling


In its simplest form,

var opt = "<option> -- Select -- </option>";  $(opt).wrap('<select />');  $('#some-container-div').html(opt); 
like image 45
Robin Maben Avatar answered Nov 01 '22 21:11

Robin Maben