Search code examples
javascripthtmldomdhtml

What is the difference between HTML Event Attributes and Assign Events Using the HTML DOM?


HTML Event Attributes:

<button onclick="displayDate()">Try it</button>

Assign Events Using the HTML DOM:

<script>

document.getElementById("myBtn").onclick = function(){ displayDate() };

</script>

What is the difference between these two ? Any advantages in using (Assign Events Using the HTML DOM) ?

Thanks


Solution

  • The advantage is you don't mess js code with html, it permits you to separate programming layers. This renders your code cleaner and less prone to bugs. A practice that complies with web accessibility rules and good programming foundations.