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.
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.
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.
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.
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With