cppit
cppit

Reputation: 4564

dilemma with on change event dropdown

Hello so here is what I am doing :

  $("#categorys").change(function () {
//$(document).on('click', '#categorys', function(){ 
        alert('changed or clicked');     
 });

when I use the .change nothing happens, no errors on the js debugger, when I use the on click event for the same dropdown it returns the alert. I would to be able to alert(this.val) once the dropdown value changes please help

Upvotes: 0

Views: 34

Answers (2)

Felix
Felix

Reputation: 38102

It's because you use event delegation for your click event, you need to do the same for change event:

$(document).on('change', '#categorys', function(){ 
    alert('changed or clicked');     
});

Btw, this behavior only happens when your select has been added dynamically.

Upvotes: 1

cppit
cppit

Reputation: 4564

I used this to solve the matter:

$(document).on('change', '#categorys', function(){ 
        alert($(this).val());     


    });

Upvotes: 0

Related Questions