Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically select an <option> within a <select> using jQuery

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>
like image 490
programmer guy Avatar asked Dec 13 '22 08:12

programmer guy


1 Answers

Try this instead:

$("#location_list").val(LID);

Setting the drop down value should do the trick.

like image 71
Shadow Wizard Hates Omicron Avatar answered Jan 31 '23 08:01

Shadow Wizard Hates Omicron