No, this is not the same. OnClick sets the property of the DOM element, where .click() adds an eventListener.
The difference between the two is that each DOM element can only have a property property at once. Therefore, if you use onClick= twice for an element, there will be only the last added, the first will be overwritten.
This will always warn 2, because the first attachment will be overwritten:
myDiv.onclick = function(){alert('1')} myDiv.onclick = function(){alert('2')}
Using .click() or .addEventListener('click', myFunction) , you can add as many functions as you want. Thus, the following warning 1 and 2:
myDiv.click(function(){alert('1')}) myDiv.click(function(){alert('2')})
The difference between jquerys .click() and .addEventListener() is that the jquery solution works in all browsers, so IE <= 8 has a different syntax ( attchEvent ). And you can unbind process all click handlers once. A normal JavaScript solution can only separate the passed function from all.
source share