lundi 5 octobre 2015

Which is better: using onclick or find element that triggered the event for handling events in jQuery?

I always wondered which is the better way of handling events in terms of code manageability, cleanliness and code reuse.

If you use the former then say a list of 10 anchor tags with click handler will have something like:

<a href="javascript:void(0);" onclick="handleEvent();">Click Me</a>
<a href="javascript:void(0);" onclick="handleEvent();">Click Me</a>
<a href="javascript:void(0);" onclick="handleEvent();">Click Me</a>

... 10 times

which looks kind of odd.

With the latter method, using anonymous function, it'd be like:

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

Aucun commentaire:

Enregistrer un commentaire