1

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>
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
soniccool
  • 5,790
  • 22
  • 60
  • 98

3 Answers3

5

Do this:

$('.formselect').val('single').trigger('change');

In your code you're trying to set a val 2 that doesn't exist. The value you're interested is actually single as you mention in your question.

Here's a demo: http://jsbin.com/eXONuhu/1/

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • 1
    So i tried your script replacing formselect with srctype and i get this error `TypeError: Cannot call method 'val' of null` – soniccool Sep 06 '13 at 19:20
1

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);
Atif
  • 10,623
  • 20
  • 63
  • 96
j08691
  • 204,283
  • 31
  • 260
  • 272
  • Ah i got an error sayingg `TypeError: Cannot call method 'val' of null` – soniccool Sep 06 '13 at 19:15
  • 3
    You're using jQuery syntax so you need to include the jQuery library. Are you? – j08691 Sep 06 '13 at 19:17
  • Well this is on a website im just trying to make an auto script its already loaded. – soniccool Sep 06 '13 at 19:18
  • Well, my solution works as you can see from the jsFiddle link I posted. You'll need to figure out if you're loading jQuery. – j08691 Sep 06 '13 at 19:18
  • Yeah i called jquery i checked still getting this error strangle when i enter it in my browser console. TypeError: Cannot call method 'prop' of null get stack: function () { [native code] } message: "Cannot call method 'prop' of null" set stack: function () { [native code] } __proto__: Error – soniccool Sep 06 '13 at 19:21
  • Well, without more code or a link to your site I'm afraid I can't do much more. – j08691 Sep 06 '13 at 19:22
  • Yeah its a router im trying to automate a script its strangely not working for this script but others it works totally fine. – soniccool Sep 06 '13 at 19:23
  • try wrapping it into IIFE `(function ($) { $('select[name="srctype"]').val('single'); }).call(this, jQuery);` – Atif Sep 06 '13 at 19:26
  • Im using jquery 1.6.2 but let me try it – soniccool Sep 06 '13 at 19:26
  • 1
    Yes it worked! `(function ($) { $('select[name="srctype"]').val('single'); }).call(this, jQuery);` worked perfectly. – soniccool Sep 06 '13 at 19:26
  • Can you update the answer to that :) ill check you right now thanks!! – soniccool Sep 06 '13 at 19:27
  • Good. Yes, you should be sure to wrap your code in a document ready call as you noted in your last comment. I'll update my answer to match. – j08691 Sep 06 '13 at 19:28
  • @j08691 - Hey sorry I accidentally edited your answer. I thought it would send you for a review. I don't know how to revert. You can revert it back or modify it. Apologies! :-) – Atif Sep 06 '13 at 19:30
0
var myVal = $('select[name="srctype"] option:eq(1)').attr('value');
$('select[name="srctype"]').val(myVal).trigger('change');

Or an optimized version

var mySelect = $('select[name="srctype"]');
var myVal = $('option:eq(1)', mySelect).attr('value');
mySelect.val(myVal).trigger('change');

Fiddle - http://jsfiddle.net/P6Ayz/1/

Atif
  • 10,623
  • 20
  • 63
  • 96