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>
Try this instead:
$("#location_list").val(LID);
Setting the drop down value should do the trick.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With