Skip to main content
Some copy editing (but there is still room for improvement).
Source Link
Peter Mortensen
  • 31.4k
  • 22
  • 110
  • 134

Difference in works, if. If you use click(), you can add several fnfunctions, but if you use an 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> 

JSJavaScript

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

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

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.

Difference in works. If you use click(), you can add several functions, but if you use an 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> 

JavaScript

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

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

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 perfoncaperformance, in any case directly using is always faster, but using of attribute you will be able to asignassign only one functinfunction.

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 perfonca, in any case directly using is always faster, but using of attribute you will be able to asign only one functin.

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.

Source Link
Anton Baksheiev
  • 2.3k
  • 2
  • 15
  • 15

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 perfonca, in any case directly using is always faster, but using of attribute you will be able to asign only one functin.