How to fire onchange event using jQuery?

I have a drop down 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 it:

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

I want to run the select onchange function when jQuery toggles a value. How can i do this? The code above does not work.

+7
source share
3 answers

You can call change() on select before the 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(); } 
+20
source
 $(document).ready(function(){ ..code.. $('#priority_filter').on('change', function(){ ..do your stuff.. } ..code.. }); 
+3
source

Try the following: When downloading

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

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

0
source

All Articles