← Back to HTML Course | Chapter 9: Reference & Advanced | Lesson 4 of 9

HTML Events

Imagine driving your car. It has sensors everywhere: if you open the door, a light turns on. If you press the horn, it makes a sound. If you step on the gas pedal, the engine revs. HTML events are those car sensors. When a visitor interacts with your website (like clicking a button, typing inside an input box, or scrolling down the page), the browser triggers an event. By connecting these events to JavaScript scripts, you can make your webpage respond instantly to user actions, turning static text pages into fully functional, interactive web apps.

What are HTML Events?

Events represent occurrences or actions that happen in the browser, typically triggered by user interactions. HTML allows you to bind event handlers directly to your tags to run JavaScript code when these actions occur.

Note: Use event listener scripts instead of inline event attributes to keep your code clean and organized.

Warning: Do not write complex, heavy code inside inline event attributes; keep your scripts modular.

Example: What are HTML Events?

markup
<button onclick="alert('Clicked')">Click me</button>

Mouse Events

Mouse events trigger scripts in response to mouse interactions, such as clicking a button (onclick), double-clicking (ondblclick), or hovering over an element (onmouseover).

Note: Use hover events to display helpful, interactive guide boxes next to your tools.

Warning: Ensure your mouse actions also work for keyboard-only and mobile users who cannot hover.

Example: Mouse Events

markup
<button onclick="console.log('clicked')" ondblclick="console.log('double-clicked')" onmouseover="console.log('hovered')">
  Hover or click me
</button>

Keyboard Events

Keyboard events trigger scripts in response to keyboard interactions, such as pressing a key down (onkeydown) or releasing it (onkeyup), which is useful for creating keyboard shortcuts.

Note: Use keyboard events to let users submit calculations quickly by pressing the Enter key.

Warning: Make sure your custom keyboard shortcuts do not conflict with standard browser or screen-reader navigation shortcuts.

Example: Keyboard Events

markup
<input type="text" onkeydown="console.log('key down')" onkeyup="console.log('key up')">

Form Events

Form events trigger scripts in response to form interactions, such as submitting a form (onsubmit), changing an input value (onchange), or typing inside a text field (oninput).

Note: Use onsubmit to validate user inputs using JavaScript before sending data to the server.

Warning: Always call preventDefault inside your onsubmit handlers to prevent the browser from reloading the page during validation.

Example: Form Events

markup
<form onsubmit="event.preventDefault()">
  <input type="text" onchange="console.log('changed')" oninput="console.log('typing')">
</form>

Window Events

Window events trigger scripts in response to browser window actions, such as when the page finishes loading (onload), when the window is resized (onresize), or when the user scrolls (onscroll).

Note: Use the onload event to initialize your scripts only after the page has finished loading.

Warning: Avoid writing heavy, complex scripts inside scroll event listeners as they can cause lag.

Example: Window Events

markup
<body onload="console.log('page loaded')" onresize="console.log('resized')" onscroll="console.log('scrolled')">
</body>
Common Mistakes
  1. Writing complex, heavy JavaScript code directly inside inline HTML event attributes.
  2. Forgetting to call preventDefault on form submit events, causing the browser to reload the page.
  3. Binding heavy, unoptimized scripts to scroll and resize events, slowing down page performance.
Chapter Summary
  • HTML events trigger JavaScript code in response to user or browser window actions.
  • Use mouse events (like onclick) and keyboard events (like onkeyup) to handle user inputs.
  • Use form events (like onsubmit) to validate data on the front-end, and window events (like onload) to initialize your scripts safely.
Browser Support

Standard HTML event triggers and attributes are supported natively by all web browsers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.