When setting a value for a dropdown in jQuery, will the .change function be called?

Whenever we set values ​​for a dropdown using

$("#dropDownId").val(giveAValueHere);

or

$('#dropDownId option:contains("' + giveTheTextHere + '")').prop('selected', true);

will the .change () function be called to call this element?

Suppose that the change () function is defined for a drop-down list and the Item is giveTheTextHerealways present in the drop-down menu.

+3
source share
2 answers

Changing an element from javascript does not raise a change event. You need to call it:

$("body").on("change", "#dropDownId", function () {
    alert("Changed");
});

or

$("#dropDownId").val(giveAValueHere).change();
0
source

A change event handler will not be called if you change the value using code, although you can trigger a change event after changing the value.

$("#dropDownId").val(giveAValueHere).trigger("change");

or

$("#dropDownId").val(giveAValueHere).change();
0
source

All Articles