zhuhang.jasper
zhuhang.jasper

Reputation: 4475

jQuery - trigger('change') vs change()

Edit: To clarify, what I want to ask is: In what scenario will you prefer one syntax over the another?

What is the difference between:

.trigger('change') and .change()

Both works as expected. Is there any scenario where the syntax will make a difference?

Upvotes: 9

Views: 21933

Answers (3)

Matze
Matze

Reputation: 114

I recommand to use .trigger('change'), hence .change() is deprecated.

Upvotes: 0

Sunil kumar
Sunil kumar

Reputation: 771

Both trigger and change are events but trigger gives you a feature to call anywhere like here on page load. If you want to call this change event on page load so u can use $(element).trigger('change').

$(element).on("change", function(){
    // statement
})

$(document).ready(function() {
    $(element).trigger('change');
});

Upvotes: 4

Rahul Tripathi
Rahul Tripathi

Reputation: 172548

The JQuery documentation for .change() says:

This method is a shortcut for .on( "change", handler ) in the first two variations, and .trigger( "change" ) in the third.

Upvotes: 7

Related Questions