HTML event attributes are on* attributes you put on elements so the page can react when something happens—a click, a form change, a key press, or focus moving into a field.
What does an event attribute do?
The attribute name names the event (onclick, onchange). The value is JavaScript that runs when that event fires. That is how a static HTML page starts feeling interactive.
Where do beginners start?
Learn onclick on buttons, onchange / onsubmit on forms, mouse enter/leave for hover demos, and onkeydown / onfocus for inputs. If you have not linked scripts yet, see JavaScript in HTML first.
Inline attributes vs script listeners
Inline on* attributes are great for learning and tiny examples. As pages grow, prefer addEventListener in a <script> so markup stays readable and you can attach more than one handler per event.
Name pattern.
Almost every event attribute starts with on + the event type: onclick, onload, oninput.
Needs JavaScript.
If scripting is blocked or disabled, event attributes do nothing. Keep critical form behavior in HTML + the server too.
Attribute pages.
Deep per-attribute notes live under HTML Attributes (many on* names are global).
Syntax
HTML Syntax
Put the handler on the element that should react:
HTML
<button type="button"
onclick="document.getElementById('msg').textContent = 'Clicked!'">
Click Me
</button>
<p id="msg">Waiting…</p>
Modern alternative (same idea, separate script):
JS
document.querySelector("#myBtn").addEventListener("click", function () {
document.getElementById("msg").textContent = "Clicked!";
});
onkeydown, onkeyup (prefer these over legacy onkeypress)
Clipboard
oncopy, oncut, onpaste
Media
onplay, onpause, onended on audio/video
Window / body
onload, onresize, onbeforeunload
Inline attribute vs addEventListener
onclick="…"
addEventListener
Where it lives
Inside HTML markup
In a script file or block
Multiple handlers
One attribute overwrites itself
Many listeners allowed
Best for
Learning, tiny demos
Apps and maintainable pages
Approach
How to Add Events in HTML
A practical workflow:
Pick the element that should react (button, input, form, …).
Choose the matching on* attribute for the user action.
Write a short JavaScript expression or call a named function.
Update the page (text, styles, show/hide) from that handler.
When the demo grows, move the same logic into addEventListener.
Goal
HTML pattern
Button feedback
<button onclick="…">
Select choice
<select onchange="…">
Validate before send
<form onsubmit="return check();">
Highlight on hover
onmouseover / onmouseout (or CSS :hover)
Try it
Live Preview
Click the button—an onclick-style handler updates the message (wired with addEventListener under the hood).
Same idea as onclick on a button in HTML.
Message
Waiting for a click…
Code
HTML Examples
Six beginner patterns: click, hover, change, submit, keydown, and focus/blur.
Example 1 — onclick
HTML
<button type="button"
onclick="document.getElementById('msg').textContent = 'Button clicked!'">
Click Me
</button>
<p id="msg">Waiting for a click…</p>
Most beginner event bugs come from mixing markup with too much script or forgetting accessibility.
size
Huge code inside onclick
Long inline scripts are hard to read and debug. Call a named function or use addEventListener.
a11y
Mouse-only handlers
Keyboard users may never fire onmouseover. Prefer buttons/links and CSS :focus-visible where possible.
form
Forgetting return false
Invalid onsubmit checks that do not return false (or call preventDefault) still navigate away.
csp
Content Security Policy
Many hosts block inline event attributes. External scripts with listeners are safer and more portable.
legacy
onkeypress
Deprecated for new work. Use onkeydown / onkeyup instead.
css
Hover via JS only
For color/background changes, CSS :hover is usually simpler than onmouseover.
Compatibility
Browser Support
HTML event attributes and the DOM Event API are Baseline web platform features. Common handlers like click, change, submit, keydown, and focus work in all modern browsers.
✓ Baseline · Widely available
HTML events
on* attributes and script listeners work across current browsers worldwide.
100%basic events
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
HTML basic eventsUniversal
Bottom line: Start with onclick, onchange, and onsubmit in HTML; graduate to addEventListener for real projects.
Remember
Key Takeaways
on* attributes run JavaScript when page events fire.
Learn with onclick / onchange / onsubmit, then expand.
addEventListener scales better than inline attributes.
One line: Attach an on* attribute (or a script listener) so HTML elements can respond to clicks, form changes, and keys.
Frequently Asked Questions
They are on* attributes in HTML markup—onclick, onchange, onsubmit, and similar—that run JavaScript when the user or browser triggers that event on an element.
onclick attributes are fine for learning and tiny demos. For real projects, prefer addEventListener in a script so markup stays clean, multiple handlers can share one event, and Content Security Policy is easier to satisfy.
Start with onclick, onchange, onsubmit, onmouseover/onmouseout, onkeydown, and onfocus/onblur. Those cover buttons, forms, hover, typing, and focus.
No. If scripting is disabled, on* handlers never run. Keep forms usable with native HTML (required, proper types) and server checks.
Many global events work on most elements, but some only make sense on specific tags—onsubmit on form, onplay on video, ontoggle on details.
In onsubmit, return false (or call event.preventDefault() in a script listener) when validation fails.