I am setting the selected value of a select list as below
$("#typeFilter").val("0");
My question is do making this, triggers $("#typeFilter").change
event?
I am setting the selected value of a select list as below
$("#typeFilter").val("0");
My question is do making this, triggers $("#typeFilter").change
event?
- 4 How about trying that out yourself and let us know too ? – gkalpak Commented May 16, 2013 at 6:21
- Have you even tried to check if it does? – Mohayemin Commented May 16, 2013 at 6:23
4 Answers
Reset to default 4No it won't trigger the change event if you want to trigger the event after setting the value use trigger
. Change event is triggered only if it is an interaction from device not with javascript.
$("#typeFilter").val("0");
$("#typeFilter").trigger('change');
Sample Demo
No, it doesn't! Unless the value is changed by user interaction, change event would not be triggered.
You can try it out yourself. Select any textbox's id. Lets say the id is "inputEmail". Try the following code:
$("#inputEmail").change(function() {
alert("changed");
});
Then try the following line of code:
$("#inputEmail").val("abc");
Notice that the alert message did not appear.
No, it doesn't trigger it as it can bee seen in this live demo
. You will need to trigger it manually after setting the value:
$("#typeFilter").change();
Nopes. It doesn't change. This is the code I used:
$(document).ready(function(){
setTimeout(function(){
$("#test").val("3");
}, 1000);
$("#test").change(function(){
alert("Changed!");
});
});
So, if you would like to trigger the change, you can use something like this:
$(document).ready(function(){
$("#test").val("3").trigger("change");
$("#test").change(function(){
alert("Changed!");
});
});