Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fire select onchange event with jQuery?

Tags:

html

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.

like image 259
csaron92 Avatar asked Apr 11 '13 09:04

csaron92


People also ask

How do I use Onchange in jQuery?

The change() method triggers the change event, or attaches a function to run when a change event occurs. Note: For select menus, the change event occurs when an option is selected. For text fields or text areas, the change event occurs when the field loses focus, after the content has been changed.

What triggers Onchange event?

The onchange JavaScript event is triggered when an element is changed and then loses focus. In the context of a textarea , this happens when the content of the textarea is modified and then the textarea loses focus because the user clicks away or presses the tab key.

Does jQuery Val trigger change event?

When you dynamically set a value in a textfield using jQuery . val(), you have to manually trigger the . change event if you want to add extra code that trigger when the value of the field change.


1 Answers

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();      
}
like image 58
Adil Avatar answered Sep 22 '22 18:09

Adil