🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to JavaScript Notes
Topic #147

JS HTML Events

HTML Events

HTML events are things that happen to HTML elements.

Examples of events:

  • An HTML button is clicked
  • A web page has finished loading
  • The mouse moves over an element
  • A keyboard key is pressed
  • An HTML input field is changed

HTML DOM allows JavaScript to react to HTML events:

<div style="width:100%;height:120px;">
<div onmouseout="this.innerHTML='Mouse Over Me'" onmouseover="this.innerHTML='Thank You'" style="background-color:#D94A38;width:170px;height:80px;margin:20px;padding-top:20px;color:#ffffff;font-weight:bold;font-size:18px;float:left;text-align:center;">Mouse Over Me</div>
<div onclick="clickMeEvent(this)" style="background-color:#D94A38;width:170px;height:80px;margin:20px;padding-top:20px;color:#ffffff;font-weight:bold;font-size:18px;float:left;text-align:center;">Click Me</div>
</div>

JavaScript Events

Often, when events happen, you may want to do something.

When JavaScript is used in HTML pages, JavaScript can react on events.

JavaScript lets you execute code when events are detected.

HTML allows event handler attributes, with JavaScript code, to be added to HTML elements.

Syntax

<element
event='some JavaScript'>

In the following example, an onclick attribute (with code), is added to a <button> element:

Example

<button onclick="document.getElementById('demo').innerHTML = Date()">The time is?</button>

In the example above, the JavaScript code changes the content of the element with id="demo".

In the next example, the code changes the content of its own element using this.innerHTML:

Example

<button onclick="this.innerHTML = Date()">The time is?</button>

Calling a JavaScript Function

JavaScript code can often be several lines long.

It is more common to use the event attribute to call a function:

Example

<button onclick="displayDate()">The time is?</button>

<script>

function displayDate() {

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

}

</script>

Common HTML Events

Here is a list of some common HTML events:

Event Description
onchange An HTML element has been changed
onclick The user clicks an HTML element
onmouseover The user moves the mouse over an HTML element
onmouseout The user moves the mouse away from an HTML element
onkeydown The user pushes a keyboard key
onload The browser has finished loading the page

The list is much longer: W3Schools JavaScript Reference HTML DOM Events.


JavaScript Event Handlers

An event handler is JavaScript code that runs when an event happens.

Event handlers can be used to handle and verify user input, user actions, and browser actions:

  • Things that should be done every time a page loads
  • Things that should be done when the page is closed
  • Action that should be performed when a user clicks a button
  • Content that should be verified when a user inputs data
  • And more ...

Many different methods can be used to let JavaScript work with events:

  • HTML event attributes can execute JavaScript code directly
  • HTML event attributes can call JavaScript functions
  • You can assign your own event handler functions to HTML elements
  • You can prevent events from being sent or being handled
  • And more ...

Using an Event Listener

Using event attributes like onclick are easy to use.

Nevertheless, using addEventListener() is the recommended way to handle events.

Not Recommended

<button onclick="displayDate()">Time is?</button>

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

<script>

// Function to display date

function displayDate() {

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

}

</script>

The addEventListener() method keeps HTML and JavaScript separated.

Highly Recommended

<button id="myBtn">Click me</button>

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

<script>

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

// Add EventListener to btn

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

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

});

</script>

Note: You will learn a lot more about events and event handlers in the HTML DOM chapters.

Want to go beyond the notes?

Join CodingNow 2.0's JavaScript course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

JS HTML Events – FAQs

Quick answers about learning JS HTML Events in JavaScript.

This free note from CodingNow 2.0 explains JS HTML Events in JavaScript — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every JavaScript topic on CodingNow 2.0, including JS HTML Events, is 100% free with no signup required.
With focused practice, most students grasp JS HTML Events in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now