Friday, April 20, 2012

how to to add options to a select from an array with jQuery

Using DOM Elements Creator plugin :
$.create('option', {'value': 'val'}, 'myText').appendTo('#mySelect');
Using the Option constructor (not sure about browser support):
$(new Option('myText', 'val')).appendTo('#mySelect');
Using document.createElement (avoiding extra work parsing HTML with $("<option></option>")):
$('#mySelect').append($(document.createElement("option")).attr("value","val").text("myText"));

No comments:

Post a Comment