javascripthtmlhtml-selectjquery-trigger

Simulate javascript to choose from dropdown


I am trying to simulate a javascript to choose from the dropdown list below. I am trying this code here but its not working.

$('select[name="srctype"]').val(2).trigger('change');

Am i doing something wrong here? Is my javascript messed up for this layout of code below?

I am trying to choose value="single"

<select name="srctype" class="formselect" onchange="typesel_change()">
    <option value="any" selected="selected">any</option>
    <option value="single">Single host or alias</option>
    <option value="network">Network</option>
    <option value="pptp">PPTP clients</option>
    <option value="pppoe">PPPoE clients</option>
    <option value="l2tp">L2TP clients</option>
    <option value="wan">WAN subnet</option>
    <option value="wanip">WAN address</option>
    <option value="lan">LAN subnet</option>
    <option value="lanip">LAN address</option>
</select>

Solution

  • Try:

    $('select[name="srctype"]').val('single')
    

    or

    $('select[name="srctype"] option:eq(1)').prop('selected',1)
    

    As you noted in your comment, this worked for you:

    (function ($) {
        $('select[name="srctype"]').val('single');
    }).call(this, jQuery);