- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- Event Attributes
HTML References
Event Attributes
Event attributes run JavaScript when something happens: a click, a key press, a form being sent. Add onclick to a button and you have your first interactive page. They are also the old way of doing it, and it helps to know why.
What is an Event?
An event is something that happens in the browser, caused by the user or by the page itself:
- A user clicks a button:
onclick - The page finishes loading:
onload - A user presses a key:
onkeydown - A form is sent:
onsubmit - The mouse moves over an element:
onmouseover
Every event attribute is the event name with on in front.
Basic Syntax
html
<element event="JavaScript code"></element>Example:
html
<button onclick="alert('Button clicked!')">Click Me</button>Click the button and an alert box pops up. The JavaScript inside the quotes runs every time the event happens.
Common Event Attributes
| Event attribute | Runs when | Example |
|---|---|---|
| onclick | The element is clicked | <button onclick="myFunction()">Click</button> |
| onload | The page or an image has finished loading | <body onload="init()"> |
| onchange | A form field's value changes and the field loses focus | <input onchange="checkValue()"> |
| oninput | The value changes, on every keystroke | <input oninput="showLength()"> |
| onmouseover | The mouse pointer moves onto the element | <div onmouseover="highlight()">Hover</div> |
| onmouseout | The mouse pointer leaves the element | <div onmouseout="removeHighlight()">Move out</div> |
| onkeydown | A key is pressed | <input onkeydown="keyPress()"> |
| onfocus | A field gets the keyboard focus | <input onfocus="highlightField()"> |
| onblur | A field loses focus | <input onblur="removeHighlight()"> |
| onsubmit | A form is about to be sent | <form onsubmit="return validateForm()"> |
Example 1: Click Event
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Event Example</title>
<script>
function showMessage() {
alert("You clicked the button!");
}
</script>
</head>
<body>
<button onclick="showMessage()">Click Me</button>
</body>
</html>The button calls a function defined in the <script> block. Calling a function keeps the attribute short, which is easier to read than writing a whole script inside the quotes.
Example 2: Form Validation Event
html
<form onsubmit="return validateForm()">
<input type="text" id="name" placeholder="Enter your name">
<button type="submit">Submit</button>
</form>
<script>
function validateForm() {
let name = document.getElementById('name').value;
if (name === "") {
alert("Please enter your name!");
return false;
}
alert("Form submitted successfully!");
return true;
}
</script>If the name field is empty, the function returns false and the form is not sent. The return in onsubmit="return validateForm()" matters: without it, the form is sent anyway.
Example 3: Mouse Events
html
<div onmouseover="this.style.background='lightgreen'"
onmouseout="this.style.background='white'"
style="width:200px; height:100px; border:1px solid gray; text-align:center; line-height:100px;">
Hover over me!
</div>The box turns light green when the mouse is over it and white again when it leaves. Inside an event attribute, this is the element itself.
Event Categories
| Category | Examples |
|---|---|
| Mouse Events | onclick, ondblclick, onmouseover, onmouseout, onmousemove |
| Keyboard Events | onkeydown, onkeypress, onkeyup |
| Form Events | onsubmit, onchange, onfocus, onblur |
| Window Events | onload, onresize, onscroll, onunload |
The better way: addEventListener
Event attributes work, but in real projects they are avoided. They mix JavaScript into HTML, you can only attach one handler per event, and strict security settings (a Content Security Policy) block them completely. The modern way keeps the HTML clean and adds the behavior from JavaScript:
The same click, with addEventListener
html
<button id="clickBtn">Click Me</button>
<script>
document.getElementById("clickBtn").addEventListener("click", function () {
alert("Hello, World!");
});
</script>Note the event name loses the on: onclick becomes "click". The full story is in JavaScript addEventListener.
Complete HTML Event Attributes Reference
Mouse Events
| Event Attribute | Description |
|---|---|
| onclick | Triggered when an element is clicked. |
| ondblclick | Triggered when an element is double-clicked. |
| onmousedown | Triggered when a mouse button is pressed down. |
| onmouseup | Triggered when a mouse button is released. |
| onmouseover | Triggered when the mouse pointer enters an element. |
| onmouseout | Triggered when the mouse pointer leaves an element. |
| onmousemove | Triggered when the mouse is moved within an element. |
| onwheel | Triggered when the mouse wheel is scrolled. |
| oncontextmenu | Triggered when the right mouse button is clicked (opens context menu). |
Keyboard Events
| Event Attribute | Description |
|---|---|
| onkeydown | Triggered when a key is pressed down. |
| onkeypress | Triggered when a key is pressed (deprecated in HTML5). |
| onkeyup | Triggered when a key is released. |
Form Events
| Event Attribute | Description |
|---|---|
| oninput | Triggered when user input is received. |
| onchange | Triggered when an element’s value changes. |
| onsubmit | Triggered when a form is submitted. |
| onreset | Triggered when a form is reset. |
| onfocus | Triggered when an element receives focus. |
| onblur | Triggered when an element loses focus. |
| onselect | Triggered when text within an input or textarea is selected. |
| oninvalid | Triggered when a form field fails validation. |
Window / Document Events
| Event Attribute | Description |
|---|---|
| onload | Triggered when the page or element is fully loaded. |
| onunload | Triggered when the page is closed or refreshed. |
| onresize | Triggered when the browser window is resized. |
| onscroll | Triggered when the user scrolls an element or page. |
| onerror | Triggered when an error occurs while loading an element (like an image). |
Drag & Drop Events
| Event Attribute | Description |
|---|---|
| ondrag | Triggered while an element is being dragged. |
| ondragstart | Triggered when drag starts. |
| ondragend | Triggered when drag ends. |
| ondragenter | Triggered when dragged element enters a valid drop target. |
| ondragleave | Triggered when dragged element leaves a drop target. |
| ondragover | Triggered when dragged element is over a drop target. |
| ondrop | Triggered when dragged element is dropped. |
Clipboard Events
| Event Attribute | Description |
|---|---|
| oncopy | Triggered when text or content is copied. |
| oncut | Triggered when text or content is cut. |
| onpaste | Triggered when text or content is pasted. |
Media Events (Audio/Video)
| Event Attribute | Description |
|---|---|
| onplay | Triggered when media starts playing. |
| onpause | Triggered when media is paused. |
| onended | Triggered when media playback ends. |
| onvolumechange | Triggered when the volume changes. |
| ontimeupdate | Triggered when the playback position changes. |
| onloadeddata | Triggered when media data is loaded. |
| onprogress | Triggered while downloading media data. |
| onerror | Triggered when media loading fails. |
Animation & Transition Events
| Event Attribute | Description |
|---|---|
| onanimationstart | Triggered when a CSS animation starts. |
| onanimationend | Triggered when a CSS animation ends. |
| onanimationiteration | Triggered when a CSS animation repeats. |
| ontransitionend | Triggered when a CSS transition finishes. |
Clipboard & Input Composition Events
| Event Attribute | Description |
|---|---|
| oncompositionstart | Triggered when text composition (IME) starts. |
| oncompositionupdate | Triggered when text composition is updated. |
| oncompositionend | Triggered when text composition ends. |
Touch & Pointer Events (Mobile)
| Event Attribute | Description |
|---|---|
| ontouchstart | Triggered when a touch point is placed on the touch surface. |
| ontouchmove | Triggered when a touch point moves. |
| ontouchend | Triggered when a touch point is removed. |
| onpointerdown | Triggered when a pointer (mouse, pen, or touch) is pressed. |
| onpointerup | Triggered when a pointer is released. |
| onpointermove | Triggered when a pointer moves. |
Miscellaneous Events
| Event Attribute | Description |
|---|---|
| onabort | Triggered when media loading is aborted. |
| onbeforeunload | Triggered before a document is unloaded. |
| onhashchange | Triggered when the URL hash changes. |
| onmessage | Triggered when a message is received via postMessage(). |
| onpopstate | Triggered when navigating browser history. |
| onstorage | Triggered when data in localStorage or sessionStorage changes. |
Good to know
onchangeon a text field fires when you leave the field, not while typing. For live updates, useoninput.onkeypressis deprecated. Useonkeydowninstead.- Events on touch screens: a tap also fires
onclick, so you rarely need separate touch events for buttons.
Event Attributes exercises
React to a click
Make a button with the id go that says Start. When it is clicked, its text becomes Running. Use an event attribute.
Result
