What is the difference between using addEventListener?

What is the main difference between using this ...

document.addEventListener('mousedown', function() { // code }, false); 

... and this?

 document.onmousedown = function() { // code } 


Will there be any other result or any reason?

+8
javascript javascript-events
source share
1 answer

onclick - a property similar to the onclick attribute can be placed in HTML. It has better browser support, however it is primitive because reassigning it overwrites the first one (for example, any property of an object).

addEventListener() , as the name implies, allows you to register multiple callbacks for an element and event type. This allows you to have multiple mousedown events for the same element. Before IE9, IE had its own attachEvent() , which is similar (you should also specify the on part with attachEvent() ).

+8
source share

All Articles