Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change event not firing for programmatic changes

I have an HTML select dropdown whose value may be edited programmatically. My change event fires when the user edits the value from the page, but not when the value is changed in the code. I need the event to fire in both cases. Any ideas on how to accomplish this?

$("#dropdownId").on('change', function () {
    //do stuff
});
...
var df = document.frmMain;
df.dropdownId.value = someValue; //change event does not fire
like image 880
Isaac Avatar asked Aug 13 '15 20:08

Isaac


2 Answers

You can use jQuery's .trigger to fire an event. In your case:

$("#dropdownId").trigger("change");
like image 194
Brian Glaz Avatar answered Nov 13 '22 11:11

Brian Glaz


You need to manually fire the event using jquery trigger function after settig the value :

 $("#dropdownId").trigger('change');
like image 4
KAD Avatar answered Nov 13 '22 11:11

KAD