Logo
HTMLCSSJavaScriptReactjsnewContactUpdates

Get started today

HTMLCSSJavaScriptReactjsnewContactUpdates

Tools

Resume BuilderQR GeneratorVS Code Editor

Connect

GitHubWhatsApp Channel
dom
exception handling

Javascript Events

By Saket Bhatnagar•June 15, 2025•Beginner to Intermediate

Table of Contents

  1. What are events in Javascript?
  2. What is Event object?
  3. What are Event listeners?
  4. how to attach Event listeners?
  5. type of events

What are events in Javascript?

  1. 1Actions performed by user on browser are refered as events.
  2. 2Whenever event occurs browser creats an object which contains all information about the event and object on which event occured.
  3. 3Example: Like if user click on <h1> tag , browser automatically creates an object which have information about h1 tag and type of event occured (here , type is 'click').

What is Event object?

  1. 1Event object is a object created by the browser when user perform some action , which holds all information about type of event occured and the element on which the event occurred.
  2. 2Event object is passed to respective event handler , every time event occured.
  3. 3So, we can access event object in callback function.

What are Event listeners?

  1. 1Event listeners are functions that wait for a specific event to occur and then execute js code (callbacks) assigned to it.
  2. 2By writting logic in callback , we can control what to do when event occurs like change text color, hide or show, etc.

how to attach Event listeners?

  1. We can attach event listeners by three ways :

    • 1As an HTML attribute
      • - In this approach , we attach event listener as a attribute in opening tag.
      • - Syntax : <tag onevent_name='function_reference()'>
      • - We have to prefix 'on' before the event name.
      • - When we pass a function , we have to function_reference and parenthesis.
      • - Example : When user clicks on div , it's color should change to red.

        1<div onclick="handleButtonClick()">Hello Honney Bunny🐰</div>
        2
        3<script>
        4
        5 function handleButtonClick() {
        6 let div = document.querySelector('div');
        7 div.style.backgroundColor='red';
        8 }
        9
        10</script>
        11
    • 2As a JS Property
      • - In this approach , first we need the reference of element then we attach listener to it (the way we add property to an object).
      • - Syntax : element.onevent_name = function_reference;
      • - We have to prefix 'on' before the event name.
      • - At the time of attaching listener to element , we just need to pass function reference to it.(listener will automatically call that function).
      • - Example : When user clicks on div , it's color should change to red.

        1<div>Hello Honney Bunny🐰</div>
        2
        3<script>
        4
        5 let div = document.querySelector('div');
        6 div.onclick = handleButtonClick;
        7
        8 function handleButtonClick() {
        9 div.style.backgroundColor='red';
        10 }
        11
        12</script>
        13
    • 3Using addEventListener Method
      • - In this approach , first we need the reference of element then we attach listener to it (the way we add property to an object).
      • - Syntax : element.addEventListener(event_name,function_reference)
      • - First argument will be the event name (no need prefix 'on') and pass function reference.
      • - Example : When user clicks on div , it's color should change to red.

        1<div>Hello Honney Bunny🐰</div>
        2
        3<script>
        4
        5 let div = document.querySelector('div');
        6 div.addEventListener('click',handleButtonClick)
        7
        8 function handleButtonClick() {
        9 div.style.backgroundColor='red';
        10 }
        11
        12</script>
        13

type of events

  1. 1

    Keyboard Events



    Event NameInfo
    keydownTriggered when a key is pressed down.
    keyupTriggered when a key is released.
    keypressTriggered when a key is pressed and released.

  2. 2

    Mouse Events



    Event NameInfo
    clickTriggered when the mouse is clicked.
    dblclickTriggered when the mouse is double-clicked.
    mousedownTriggered when a mouse button is pressed down.
    mouseupTriggered when a mouse button is released.
    mousemoveTriggered when the mouse pointer moves.

  3. 3

    Form Events



    Event NameInfo
    submitTriggered when a form is submitted.
    resetTriggered when a form is reset.
    changeTriggered when the value of a form element changes.

Share this article

Last updated: July 15, 2025

Join Our Community

Login to Join

© 2025 Saket Bhatnagar. All rights reserved.

    ☕