csaron92
csaron92

Reputation: 1045

How to fire select onchange event with jQuery?

I have a dropdown list:

<select size="1" name="filter"id="priority_filter" onchange="filter_user_trainings();">
   <option value="all">All</option>
   <option value="optional">Optional</option>
   <option value="mandatory">Mandatory</option>
   <option value="essential">Essential</option>
   <option value="custom">Custom</option>
</select>

In a function I call these:

if(db==0 || db==1 ||db==2)
{
   $("#priority_filter").val('custom');
}

I want to fire the select onchange function when the jQuery switches the value. How can I do this? The code above does not work.

Upvotes: 9

Views: 34453

Answers (3)

Adil
Adil

Reputation: 148110

You can call change() on select to first or .trigger("change");

if(db==0 || db==1 ||db==2)
{
   $("#priority_filter").val('custom');    
   $("#priority_filter").change();    
}

OR

if(db==0 || db==1 ||db==2)
{
   $("#priority_filter").val('custom').change();      
}

Upvotes: 28

steo
steo

Reputation: 4656

$(document).ready(function(){

 ..code..
$('#priority_filter').on('change', function(){
     ..do your stuff..
 } 
..code..

});

Upvotes: 3

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Try this: On load

 $("#priority_filter").change(function(){
    var val = $("#priority_filter").val();
    //alert(val);
    //your code
});

Demo Here: http://jsfiddle.net/j8DPN/

Upvotes: 0

Related Questions