0

I'm learning javascript, and I've seen more than one way of registering click events:

$(DOMelement).click(function() {});
$(DOMelement).on('click',function() {});
(DOMelement).addEventListener('click', function() {});

Can someone tell me what's the best practice for event registration? Or the major advantage/disadvantage of each one? I'm asuming on() and click() are very similar, but since I'm just getting started I would like to know which is the most used so I can start writing efficient code from the start. Also, is it still a good practice to do the html onclick registration?

<button onclick="function()"></button>
4
  • 1
    $(DOMelement).addEventListener isn't a function. addEventListener is a native DOM Element function and you must unwrap the element from jQuery to use it: $(DOMelement)[index].addEventListener Commented Oct 20, 2014 at 17:13
  • 1
    You should try to keep your javascript out of your HTML as a general rule of thumb, and the three registrations are essentially the same. #3 is not jQuery, but used correctly it will accomplish the same thing. Commented Oct 20, 2014 at 17:13
  • So should I be using the addEventListener over the first ones? Commented Oct 20, 2014 at 17:20
  • So basically you are asking what is the difference between jQuery and native DOM? Or whether you should use jQuery or not? "is it still a good practice to do the html onclick registration" I don't think it ever was good practice. Commented Oct 20, 2014 at 17:35

2 Answers 2

1

$(DOMelement).click(function() {}); is the shortcut of $(DOMelement).on('click',function() {});. It's a little bit less character, but $(DOMelement).on('click',function() {}); is faster.

$(DOMelement).addEventListener('click', function() {}); will never work because it is a native JavaScript function. You need to use an HTML element, not a jQuery one. That would work :

$(DOMelement)[0].addEventListener'click', function() {});

This is the fastest one because it is native, but there is some support issue, like in IE 8 and lower, it will throw an error because it use .attachEvent().

You last solution (<button onclick="function()"></button>) is the worst possible solution since it is bad for re-utilisability, make a lot of repetition when you have multiple element and also is a bad practice.

Basically, you should always use (when jQuery is loaded) .on().

Sign up to request clarification or add additional context in comments.

2 Comments

Internet Explorer 9, and above, implement addEventListener() (check the compatibility: EventTarget.addEventListener()).
@sgarcia performance wise, yes! Also, .on() can be used for event delegation.
0

These two use jQuery to bind the event to a callback and are an alias of the same exact function (.on):

$(DOMelement).click(function() {});
$(DOMelement).on('click',function() {});

This one uses the browser's api to bind it (a little lower level):

DOMelement.addEventListener('click', function() {});

The last one also works similar to the browser api way but it frowned upon:

<button onclick="function()"></button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.