SyntaxStudy
Sign Up
HTML Beginner 4 min read

HTML Event Attributes

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.

Example
<!-- 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>
Pro Tip

Use addEventListener() in external JS files rather than inline event attributes for maintainability.