Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.click() vs onClick

I have a huge jQuery application, and I'm using the below two methods for click events.

First method

HTML

<div id="myDiv">Some Content</div> 

jQuery

$('#myDiv').click(function(){     //Some code }); 

Second method

HTML

<div id="myDiv" onClick="divFunction()">Some Content</div> 

JavaScript function call

function divFunction(){     //Some code } 

I use either the first or second method in my application. Which one is better? Better for performance? And standard?

like image 527
Techie Avatar asked Sep 27 '12 18:09

Techie


People also ask

What is difference between click and Onclick in jQuery?

So onclick creates an attribute within the binded HTML tag, using a string which is linked to a function. Whereas . click binds the function itself to the property element.

Is click same as onclick?

click is a function on HTML elements you can call to trigger their click handlers: element. click(); onclick is a property that reflects the onclick attribute and allows you to attach a "DOM0" handler to the element for when clicks occur: element.

Is jQuery click deprecated?

click() shorthand is deprecated at jQuery 3 The . on() and . trigger() methods can set an event handler or generate an event for any event type, and should be used instead of the shortcut methods.

What is click in jQuery?

jQuery click() MethodThe click event occurs when an element is clicked. The click() method triggers the click event, or attaches a function to run when a click event occurs.


1 Answers

Using $('#myDiv').click(function(){ is better as it follows standard event registration model. (jQuery internally uses addEventListener and attachEvent).

Basically registering an event in modern way is the unobtrusive way of handling events. Also to register more than one event listener for the target you can call addEventListener() for the same target.

var myEl = document.getElementById('myelement');  myEl.addEventListener('click', function() {     alert('Hello world'); }, false);  myEl.addEventListener('click', function() {     alert('Hello world again!!!'); }, false); 

http://jsfiddle.net/aj55x/1/

Why use addEventListener? (From MDN)

addEventListener is the way to register an event listener as specified in W3C DOM. Its benefits are as follows:

  • It allows adding more than a single handler for an event. This is particularly useful for DHTML libraries or Mozilla extensions that need to work well even if other libraries/extensions are used.
  • It gives you finer-grained control of the phase when the listener gets activated (capturing vs. bubbling)
  • It works on any DOM element, not just HTML elements.

More about Modern event registration -> http://www.quirksmode.org/js/events_advanced.html

Other methods such as setting the HTML attributes, example:

<button onclick="alert('Hello world!')"> 

Or DOM element properties, example:

myEl.onclick = function(event){alert('Hello world');};  

are old and they can be over written easily.

HTML attribute should be avoided as It makes the markup bigger and less readable. Concerns of content/structure and behavior are not well-separated, making a bug harder to find.

The problem with the DOM element properties method is that only one event handler can be bound to an element per event.

More about Traditional event handling -> http://www.quirksmode.org/js/events_tradmod.html

MDN Reference: https://developer.mozilla.org/en-US/docs/DOM/event

like image 65
Selvakumar Arumugam Avatar answered Sep 17 '22 17:09

Selvakumar Arumugam