Skip to main content
deleted 61 characters in body
Source Link
Nickolay
  • 32.5k
  • 13
  • 111
  • 195

Using DOM Elements Creator plugin (my favorite):

$.create('option', {'value': 'val'}, 'myText').appendTo('#mySelect'); 

UsingIf you don't have to support old IE versions, using the Option constructorOption constructor is clearly the way to go, (not sure about browser support)a readable and efficient solution:

$(new Option('myText', 'val')).appendTo('#mySelect'); 

Using document.createElement (avoiding extra work parsing HTML with $("<option></option>"))It's equivalent in functionality to, but cleaner than:

$('#mySelect').append($(document.createElement("option")"<option></option>"). attr("value", "val").text("myText")).appendTo('#mySelect'); 

Using DOM Elements Creator plugin (my favorite):

$.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")); 

If you don't have to support old IE versions, using the Option constructor is clearly the way to go, a readable and efficient solution:

$(new Option('myText', 'val')).appendTo('#mySelect'); 

It's equivalent in functionality to, but cleaner than:

$("<option></option>").attr("value", "val").text("myText")).appendTo('#mySelect'); 
Source Link
Nickolay
  • 32.5k
  • 13
  • 111
  • 195

Using DOM Elements Creator plugin (my favorite):

$.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"));