Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 returning nothing, not null or undefined just a blank alert window

However when I alert it out the attr(value) is undefined.

Any ideas on what i'm missing?

Here is my code:

<script type="text/javascript">
            $(document).ready(function() {

                var roomID = "0"
                $('.dropone').load('ajaxdropdown.aspx');
                $('.droptwo').load('ajaxdropdown.aspx?drpType=room&roomid=' + roomID);

                $('.dropone').change(function() {
                var ts = new Date().getTime();
                alert($(this).val)

                    $(".droptwo").empty();
                    $(".droptwo").load("ajaxdropdown.aspx?drpType=room&roomid=" + $(this).attr("value") + "&ts=" + ts);
                });

            });        
        </script>
like image 964
MissCoder87 Avatar asked Aug 16 '12 15:08

MissCoder87


People also ask

What event is generated when a dropdown value is changed?

The change event is fired for <input> , <select> , and <textarea> elements when the user modifies the element's value.


2 Answers

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());
});
like image 132
Shyju Avatar answered Nov 15 '22 10:11

Shyju


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

  // OR

  var val = this.value;
})
like image 45
thecodeparadox Avatar answered Nov 15 '22 09:11

thecodeparadox