How do you create a dropdown list dynamically using jQuery? By dropdown list, I mean a <select> with its associated <option> values.
3 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
3 Comments
vinodh
c-sharpcorner.com/UploadFile/dhananjaycoder/… - Above link is explaining how the above answer working step by step .. It will helpful. Just look into that. :)
singhswat
in my case do as below... How do I append it to the newly added html element ? I mean s.appendTo ( what?) var table = document.getElementById("demoGrid"); var row = table.insertRow(1); var cell1 = row.insertCell(0); var cell2 = row.insertCell(1); cell1.innerHTML = "<input type='text' />" // I want dropdown list to be shown here for users to select
Felix Kling
@singhswat:
s.appendTo(cell1).
dropdown list? did you mean the<select>tag?