---
title: "Explain the HTML Events."  
description: "HTML events are often used to trigger JavaScript functions, allowing dynamic behavior and interactivity in web pages."  
author: "Ashutosh Patel"  
published: 2024-06-10  
updated: 2024-06-10  
canonical: https://www.mindstick.com/articles/336061/explain-the-html-events  
category: "html"  
tags: ["html", "html5", "html element", "html events"]  
reading_time: 3 minutes  

---

# Explain the HTML Events.

### HTML Events

HTML events are actions or events that occur in [interaction](https://www.mindstick.com/forum/23302/sending-email-w-o-user-interaction) with a [web page](https://www.mindstick.com/forum/12863/how-to-insert-a-video-in-asp-dot-net-web-page-in-visual-studio-2010). This information can take various forms such as user actions, browser actions, or external events. HTML events are often used to trigger [JavaScript](https://www.mindstick.com/forum/12869/how-to-c-sharp-and-javascript-email-validation-regex) [functions](https://www.mindstick.com/forum/161928/explain-the-python-functions-with-explanation), resulting in dynamic actions and interactions in [web pages](https://answers.mindstick.com/qa/35620/how-would-you-change-the-format-of-all-the-phone-numbers-in-10-000-static-html-web-pages).

####

#### Types of HTML Events

Here are some common types of HTML events given below,

#### onclick

This event occurs when a user **clicks** on an [HTML elements](https://answers.mindstick.com/qa/93760/how-to-find-the-html-elements-by-using-javascript).

## Syntax-

```html
<button onclick="myFunction()">Click Here</button>
```

#### onmouseover/onmouseout

Occurs when the mouse pointer **moves to/from** an element.

## Syntax-

```html
<img src="image.jpg" onmouseover="showDetails()" onmouseout="hideDetails()">
```

#### onkeydown/onkeyup/onkeypress

When a keyboard key is **pressed/released/pressed** and **released** when [attention](https://answers.mindstick.com/qa/45144/what-is-a-calling-attention) is on an element.

## Syntax-

```html
<input type="text" onkeydown="myFunction(event)">
```

#### onload

Occurs when an object is **loaded** (e.g., a page, image, or external file).

## Syntax-

```html
<body onload="loadFunction()">
```

#### onsubmit

Occurs when the form is **submitted**.

## Syntax-

```html
<form onsubmit="return validateForm()">
```

#### onchange

Occurs when the value of an element **changes** (works with input fields, works with selected elements, etc.).

## Syntax-

```html
<input type="text" onchange="updateValue()">
```

#### onfocus/onblur

Occurs when an element **gains/loses** focus.

## Syntax-

```html
<input type="text" onfocus="highlight()" onblur="removeHighlight()">
```

These events can be handled inline (directly in an HTML element) or using [event listeners](https://www.mindstick.com/forum/159117/how-do-you-handle-events-using-jquery-and-attach-event-listeners-to-elements) in JavaScript. Event listeners offer greater flexibility and are often preferred for handling events in modern [web development](https://www.mindstick.com/services/web-development). Additionally, HTML events can bubble or eclipse, affecting how events are handled when multiple objects are nested within each other.

## Example-

Here is a basic example to demonstrate the use of HTML events,

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Events Example</title>
<style>
  /* Styling for center all the html elements */
  .html-events{
    text-align: center;
  }
</style>
</head>
<body>

<div class="html-events">
  <!-- HTML Button with onclick event -->
  <button onclick="greet()">Click Here</button>

  <br><br>

  <!-- HTML Input field with onchange event -->
  <label for="myInput">Type something:</label>
  <input type="text" id="myInput" onchange="showValue()">

  <!-- HTML paragraph with onmouseover and onmouseout events -->
  <p id="mouse-over" onmouseover="showDetails()" onmouseout="hideDetails()">Mouse Pointer Over Me</p>
</div>

<script>
  // JavaScript function to handle the onclick event
  function greet() {
    alert('You clicked me!');
  }

  // JavaScript function to handle the onchange event
  function showValue() {
    var inputElement = document.getElementById('myInput');
    alert('You typed: ' + inputElement.value);
  }

  // JavaScript function to handle the mouse over event
  function showDetails(){
    // add backgound color when mouse over
    document.getElementById("mouse-over").style.backgroundColor = "yellow";
  }

  // JavaScript function to handle the mouse out event
  function hideDetails(){
    // remove the backgournd color when mouse out
    document.getElementById("mouse-over").style.backgroundColor = "";
  }

</script>
</body>
</html>
```

## Output-

![Explain the HTML Events.](https://www.mindstick.com/mindstickarticle/c6702307-6e98-407d-af59-52418c8b5450/images/98d88dd6-2266-429d-bcca-5b5e0ca95327.jpg)

**Also, Read:** [Creating responsive drop-down in HTML](https://www.mindstick.com/articles/336055/creating-responsive-drop-down-in-html)

---

Original Source: https://www.mindstick.com/articles/336061/explain-the-html-events

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
