0

I've been trying to change a drop-down list's value (as well as the UI) based on another drop-down list's value as follows, but with no success. Any advice?

    $("[name=firstlist]").change(function(){
        if ( $("[name=firstlist]").val() == '2' ) {
            $("#id-secondlist").val("4");
        }
    });

Thanks.

hello_its_me
  • 743
  • 2
  • 19
  • 52

2 Answers2

0

You are missing element itself in your code,

try this,

 $("select[name=firstlist]").change(function(){
        if ( $(this).val() == 2 ) {
            $("select#id-secondlist").val('4');
        }
    });
ScanQR
  • 3,740
  • 1
  • 13
  • 30
0

Are you actually using all those backslashes in your code? If you use console.log you would have seen the error message.

Anyway, since jquery v?.? you need to put attribute selector values inside quotes.

$("[name='firstlist']").change(function() {
console.log(this.value);
  if ($("[name='firstlist']").val() == '2') {
    $("#id-secondlist").val("4");
  }
});
yezzz
  • 2,990
  • 1
  • 10
  • 19
  • attribute selectors are not required to be in quotes. – ScanQR Nov 26 '16 at 14:42
  • Be aware I was talking about attribute selector *values*. You're right they are not required, but only [under certain conditions](http://stackoverflow.com/questions/30595119/the-difference-between-quoted-and-unquoted-attribute-selector-in-css), though it's recommended to use them. As for the jquery I was talking about, that was actually a value that must be quoted, but jquery upto v2.1.3 supported it without quotes. Change to v.2.2.0 and it will give you an error. [Demo](http://jsfiddle.net/zfpbh4rj/) – yezzz Nov 26 '16 at 16:13
  • That was not it.. I need to change the select value after it in particular was loaded. Do you know how? thanks – hello_its_me Nov 27 '16 at 15:04
  • I don't understand what you mean by "after it in particular was loaded". Are you using ajax to load a select element or something? – yezzz Nov 27 '16 at 15:28
  • And post your html like techbreak said, otherwise this will just be guesswork – yezzz Nov 27 '16 at 15:35