Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery to preselect a dropdown value

I have this dropdown below which I have taken out of the actual page I need to work on at http://www.howtoclone.co.uk.gridhosted.co.uk/plasmid-builder where I pass the product ID value from another page to the jquery below to preselect the dropdown 'value'. When I just run this below in isolation it works.

<select name="0product_id[]" class="groupSelect" id="groupsel_0" onchange="productbuilder.update(this.value,0);">
    <option value="0" class="notag" id="id0_0">--Select--</option>
    <option class="notag" value="338" id="id0_338"  >Dual Promoter Puromycin Expression Plasmid - pSF-CMV-PGK-Puro  > £114.00</option>
    <option class="notag" value="282" id="id0_282"  >EMCV IRES Puromycin Expression Plasmid - pSF-CMV-EMCV-Puro  > £114.00</option>
    <option class="notag" value="265" id="id0_265"  >FMDV IRES Puromycin Expression Plasmid - pSF-CMV-FMDV-Puro  > £114.00</option>
    <option class="notag" value="101" id="id0_101"  >Puromycin Selection Plasmid - pSF-CMV-Ub-Puro AscI  > £114.00</option>
    <option class="notag" value="105" id="id0_105"  >Puromycin Selection SV40 Ori Plasmid - pSF-CMV-Ub-Puro-SV40 Ori SbfI  > £114.00</option>
    <option class="notag" value="323" id="id0_323"  >Rous Sarcoma Virus Promoter Puromycin Expression Plasmid - pSF-CMV-RSV-Puro  > £114.00</option>
</select>

and This is the snippet which works to change the value on load:

<script>    
    $( document ).ready(function() {
    $("#groupsel_0 option[value=105]").attr("selected", "selected");    
    });
</script> 

As soon as I upload it to the site it has no effect. Can anyone help?

like image 505
Alperian Avatar asked Dec 02 '22 18:12

Alperian


1 Answers

If you know the value, why not just set it:

$( document ).ready(function() {
    $("#groupsel_0").val("105");    
});

FIDDLE

like image 198
adeneo Avatar answered Dec 25 '22 02:12

adeneo