javascript - Dynamically set option selected -
how can set option value selected dynamically? have tried same by,
function editfromdb(optval) { $.get("/myend", function(mydata) { var s = $('<select id="' + optval + '" name="selectname" ></select>'); $(mydata).each(function(iter, element){ $('<option></option>', { value: elem.name, text: elem.name }).appendto(s); }); $("#" + optval).val(optval); // ...
just add " selected : true"
$('<option></option>', { value:"lol2", text: "yo2", selected : true }).appendto($("#test"));
two ways use :
$(mydata).each(function(iter, element){ if (your_condition_for_selected) { $('<option></option>', { value: elem.name, text: elem.name, selected : true }).appendto(s); }); } else { $('<option></option>', { value: elem.name, text: elem.name }).appendto(s); }); }
or can :
$(mydata).each(function(iter, element){ if (your_condition_for_selected) { $('<option></option>', { value: elem.name, text: elem.name, selected : your_boolean_condition }).appendto(s); }); }
Comments
Post a Comment