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 the difference between them is?
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.
- 1Bitbucket and Visual Studio
- 1How to dump and restore database in MongoDB
- 1How to compress and extract files/folders using the tar command on Ubuntu
- 1How To Create a New User and Grant Permissions in MySQL
- 2Abstract and Virtual Methods
- 0What is the difference between custom dimensions and metrics in Google Analytics
- 5How can I rename git local and remote branch name?
- 1Events and delegates in C#
- 1how to write function to generate an array between two integers
- 1*ngIf and *ngFor on same element causing error
- 1How to make my footer cover 100% height and width of the page from top?