What is the difference between addEventListener and onclick?Ask a question
There are 2 examples that adding a click event listener:
var h = document.getElementById(“a”); .onclick = dothing1; .addEventListener(“click”, dothing2);
The code above resides together in a separate .js file, and they both work perfectly.
But I wonder what’s the difference between them?
onclick() and addEventListener() methods functionality are same. But the difference between that is below.
We can use onclick method only on elements like button element(<button>), anchor element(<a>). But we can not use it directly on div(<div></div>) element, span element etc.
We can use addEventListener in every elements by register that element in addEventListener() method.
- 0C++ book guide and list
- 0undefined behavior and sequence points
- 1main function signature in C and C++
- 0What is the copy-and-swap idiom
- 0Pointless if-else and do-while in
- 0Difference between copy initialization and direct initialization
- 0C++: difference between 'struct' and 'typedef struct'
- 0public, protected and private inheritance
- 0Meanings of zero-, value- and default-initialization
- 0How do malloc() and free() work?
- 0What are external and internal linkage