UpTest > Web Development > jQuery Test

Test: jQuery Test

Question 83: Prev Next
Which of the following is the best method for adding options to a select from a JSON object using jQuery?
selectValues = { "1": "test 1", "2": "test 2" };

for (key in selectValues) {
  if (typeof (selectValues[key] == 'string') {
    $('#mySelect').append('<option value="' + key + '">' + selectValues[key] + '</option>');
  }
}
$.each(selectValues, function(key, value) {   
     $('#mySelect')
         .append($("<option></option>")
         .attr("value",key)
         .text(value)); 
});
$.each(selectValues, function(key, value) {   
     $('#mySelect')
         .append($("<option>")
         .attr("value",key)
         .text(value)); 
});
$.each(selectValues, function(key, value) {   
     $('#mySelect')
         .append($("<option>")
         .text(value)); 
});
Please login to submit your answer.