Harry
Harry

Reputation: 489

Is .bind() or .click() better to bind a click event handler?

To add a click event handler to an element, is .bind('click') or .click better? What makes the difference? Any performance aspects?

Upvotes: 0

Views: 154

Answers (1)

James Allardice
James Allardice

Reputation: 165951

There is no difference. Internally, click just calls on, and bind also just calls on. So for a very minor speed increase, just use on:

$("#someId").on("click", function () {
    //Do stuff
});

Here's the relevant part of the jQuery 1.7.2 source for the .click() method:

return arguments.length > 0 ? this.on(name, null, data, fn) : this.trigger(name);

And the source of the .bind() method:

return this.on(types, null, data, fn);

If you're using jQuery below version 1.7...

Note that the .on() method was introduced in jQuery 1.7. If you are using an older version, .click() will internally just call .bind() instead. Here's the source of .click() from 1.6.2:

return arguments.length > 0 ? this.bind(name, data, fn) : this.trigger(name);

Upvotes: 5

Related Questions