Skip to content Skip to sidebar Skip to footer

Jquery Get The Selected Dropdown Value On Change

I'm trying to get the value of a dropdown on change (and then change the values in a second dropdown). EDIT: Thanks for all the replies, i've updated to add the () but the code is

Solution 1:

val is a method, not a property.

use it like val()

If you are using it many places, i would assign it to a local variable and use it thereafter.

Also you can use the $.now() function to get the unique time stamp. It is equal to DategetTime();

$('.dropone').change(function() {    
    var item=$(this);
    alert(item.val())
    $(".droptwo").empty();
    $(".droptwo").load("ajaxdropdown.aspx?drpType=room
                        &roomid=" +item.attr("value") + "&ts=" + $.now());
});

Solution 2:

$('.dropone').change(function() {
  varval = $(this).val(); 

  // ORvarval = this.value;
})

Solution 3:

You must obtain the value using a method, not a property. Use this:

alert($(this).val())

Solution 4:

Add round brackets to your val: alert($(this).val())

Solution 5:

You can also obtain custom attributes from the dropdown as below;

     $('.someclass').change (function () { 
              var val = this.value;
              alert(val);                                       //selected valuevar element = $(this).find('option:selected');  // assign selected elementvar myTag = element.attr("aTag");              // get attribute by namealert(myTag);
    });


<optionname='somename'id='someid'aTag='123'value='XYZ'>XYZ</option>

Post a Comment for "Jquery Get The Selected Dropdown Value On Change"