Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create select list

Does anyone know of a technique to programmatically create an HTML select list including options using JQuery?

like image 834
sisko Avatar asked Jul 06 '11 19:07

sisko


People also ask

How to create dynamic select list in html?

To add a dropdown list dynamically, you would need to create the HTML <select> element, its label and optionally a <br> tag. In pure JavaScript, you can use the document. createElement() method to programmatically create a dropdown list. Then you can call the Node's appendChild() method or jQuery's .

How to create dropdown with 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.

How to create drop-down list in 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.


1 Answers

var arr = [   {val : 1, text: 'One'},   {val : 2, text: 'Two'},   {val : 3, text: 'Three'} ];  var sel = $('<select>').appendTo('body'); $(arr).each(function() {  sel.append($("<option>").attr('value',this.val).text(this.text)); }); 
like image 171
js1568 Avatar answered Sep 20 '22 21:09

js1568