HTML Event Attributes
HTML elements support inline event attributes like onclick, onchange, and onsubmit that run JavaScript when the event fires.
While easy to write, inline event attributes mix HTML and JS and are harder to maintain.
HTML elements support inline event attributes like onclick, onchange, and onsubmit that run JavaScript when the event fires.
While easy to write, inline event attributes mix HTML and JS and are harder to maintain.
<!-- Inline event attributes (avoid in production) -->
<button onclick="alert('Clicked!')">Click me</button>
<input onchange="validateInput(this)" type="text">
<!-- Preferred: addEventListener in JS file -->
<button id="myBtn">Click me</button>
<script>
document.getElementById("myBtn").addEventListener("click", () => alert("Clicked!"));
</script>
Use addEventListener() in external JS files rather than inline event attributes for maintainability.