logo Learn HTML
  • Home
  • Tutorials
  • Practice
  • Quiz
  • Projects
  • About
  • Contact

HTML Course

  • HTML HOME
  • HTML Introduction
  • HTML Basic
  • HTML Element
  • HTML Attributes
  • HTML Headings
  • HTML Paragrapha
  • HTML Style
  • HTML Formattin
  • HTML Quotations
  • HTML Colors
  • HTML Css
  • HTML Links
  • HTML Image
  • HTML Favicon
  • Page Tittle
  • HTML Tables
  • HTML Lists
  • Block & Inline
  • HTML Div
  • HTML Classes
  • HTML Id
  • HTML Buttons
  • HTML Iframes
  • HTML File Paths
  • HTML Head
  • HTML Layout
  • HTML Symbols
  • HTML Emojis
  • HTML URL Encode
  • HTML VS XHTML
  • HTML FROM
  • HTML Form
  • Form Attributes
  • Form Elements
  • HTML Input Types
  • HTML Input Attributes
  • Input Form Attributes
  • HTML Graphics
  • HTML Canvas
  • HTML SVG
  • HTML Media
  • HTML Media
  • HTML Video
  • HTML Audio
  • HTML Plug-ins
  • HTML Youtube
  • HTML APIS
  • HTML Web APIS
  • HTML Geolocation
  • HTML Drag & Drop
  • Web Storage
  • Web Workers
  • HTML SSE
  • HTML Examples
  • HTML Examples
  • HTML Editor
  • HTML Quiz
  • HTML Exercises
  • HTML References
  • HTML Tag List
  • HTML Attributes
  • HTML Global Attributes
  • HTML Brower Support
  • HTML Events
  • HTML Colors
  • HTML Canvas
  • HTML Audio & Video
  • HTML Doctypes
  • HTML Character Sets
  • HTML URL Encode
  • Language Codes
  • HTTP Messages
  • HTTP Methods
  • PX to EM Converter
  • keyboard Shortcuts

Understanding HTML Events

An HTML Event is something that happens to an HTML element. When JavaScript is used in HTML pages, the browser can "react" to these events.

An event can be something the browser does or something a user does.


1. What are HTML Events?

Events are signals that something has occurred. Here are some common examples:

  • A web page has finished loading.
  • An HTML input field was changed.
  • An HTML button was clicked.
  • A user pressed a key on the keyboard.

2. How to Handle Events

You can execute JavaScript code when an event occurs by adding an event handler attribute to an HTML element.


A. Inline Event Handler

The simplest way is to add the code directly to the HTML tag:

<button onclick="this.innerHTML = 'You clicked me!'"> Click Here </button>

B. Calling a Function

For better organization, it is recommended to call a function defined in a <script> tag:


<button onclick="displayDate()">> What is the time? </button>
<script>

function displayDate() {

document.getElementById("demo").innerHTML = Date();

}

<script>
<p id="demo"> </p>

3. Common HTML Events

Here is a list of the most frequently used events that every web developer should know:


Event Description Use Case
onclick The user clicks an HTML element. Buttons, links, and navigation menus.
onchange An HTML element has been changed. Validating text in an input field.
onmouseover The user moves the mouse over an element. Showing tooltips or changing colors.
onmouseout The user moves the mouse away from an element. Resetting styles after a hover.
onkeydown The user pushes a keyboard key. Creating shortcuts or character limits.
onload The browser has finished loading the page. Initializing scripts or animations.

4. Why Use Events?

Events allow your website to be interactive. Without events, a website is just a static document. With events, you can:

  • Verify user input before sending it to a server.
  • Create dropdown menus and pop-up windows.
  • Change the appearance of elements dynamically.
  • Track user behavior for analytics.

5. Best Practice: Event Listeners

While inline attributes like onclick are easy, modern developers prefer using addEventListener(). This keeps your HTML clean and allows you to add multiple events to a single element.

const myButton = document.getElementById("myBtn");


myButton.addEventListener("click", function() {

alert("Hello World!");

});


Summary Checklist for Your Site

  1. Identify the Trigger: Decide what action the user should take (click, hover, type).

  2. Define the Action: Write the JavaScript function that should run.

  3. Connect Them: Link the event to the element using an attribute or a listener.

Try It Your Self

Sign Up For New
Update

HTML Basic
  1. HTML Basic
  2. HTML Element
  3. HTML Attributes
  4. HTML Headings
  5. HTML Paragrapha
  6. HTML Tables
  7. HTML Lists
HTML Advance
  1. HTML Div
  2. HTML Classes
  3. HTML Id
  4. HTML Buttons
  5. HTML Iframes
  6. HTML Symbols
  7. HTML Emojis