I have a situation where I need to programatically select an option within a select. I cannot seem to locate an answer and my brain is turning to mush... This is what I have been trying (and various other variations).
$("#location_list option[value='"+LID+"']").attr("selected", "selected");
Any help would be GREATLY appreciated!
Thanks!
Here is the surrounding code...
function loc_commitData(location_data, update_loc)
{
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
else
{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
populate_loc_list('<?php echo $CompanyID; ?>', $("input:radio[name='locationstatus']:checked").val());
bind_events_to_location_list();
$("#location_list").val(xmlhttp.responseText);
}
}
xmlhttp.open("GET", "includes/save_location_data.php?" + location_data, true);
xmlhttp.send();
};
Here is the HTML snippet where the location_list is inserted within the span tags...
<fieldset style="margin-right: 15px;">
<legend>Locations</legend>
<span id="location_list_span"> THIS IS WHERE location_list GETS INSERTED ON THE FLY AFTER THE PAGE IS CREATED BY PHP</span>
</fieldset>
.attr()is a bad thing for properties. Better use.prop('selected', true_or_false)if you want to programmatically select something. The same applies for checkboxes and theircheckedproperty. But anyway, Shadow Wizard's solution is much better.alert(xmlhttp.responseText)and see for yourself.@when replying to comment e.g.@Shadowwill notify me.. I saw this just by chance now. Anyhow,.val("3")will select any option with value of 3 i.e.<option value="3">some text here</option>so post code forpopulate_loc_listand we might see better.