Skip to main content

Difference in works, if you use click() you can add several fn, but if you use attribute only one function will be executed - the last one

DEMO

HTML

<span id="JQueryClick">Click #JQuery</span> </br> <span id="JQueryAttrClick">Click #Attr</span> </br> 

JS

$('#JQueryClick').click(function(){alert('1')}) $('#JQueryClick').click(function(){alert('2')}) $('#JQueryAttrClick').attr('onClick'," alert('1')" )//this doesnt work $('#JQueryAttrClick').attr('onClick'," alert('2')" ) 

If we are talking about performance, in any case directly using is always faster, but using of attribute you will be able to assign only one function.

Anton Baksheiev
  • 2.3k
  • 2
  • 15
  • 15