Interactive pages respond to the user. Each action, such as a click or a key press, is an event, and JavaScript can run code when it happens.

What an Event Is

An event is something that happens on the page: the user clicks, presses a key, submits a form, or resizes the window. The browser creates an event for each of these, and a script can listen for it.

Definition: Event. An action or occurrence on a page, such as a click, a key press or a page load.

Table 3.5: Common events

Group Events Fires when
Mouse click, dblclick, mouseover, mouseout The pointer acts on an element
Keyboard keydown, keyup A key is pressed or released
Form submit, change, input, focus The user works with a form
Window load, resize, scroll The window or page changes

Event Listeners

An event listener is a function that runs when a chosen event happens on an element. The method addEventListener attaches one. It takes the event name and the function: button.addEventListener("click", handler). The function that responds is called the event handler. It receives an event object that describes what happened, such as which key was pressed or which element was clicked.

Definition: Event listener. A function that runs when a chosen event happens on an element.

Good Practice

Attaching a listener with addEventListener is better than writing onclick inside the HTML tag, because it keeps behaviour out of the markup and allows several listeners on one element. When passing a handler, write its name without brackets. With brackets, the function would run at once instead of waiting for the event.

Example 3.5: A click counter

<button id="btn">Clicked 0 times</button>
<script>
  let count = 0;
  const btn = document.getElementById("btn");
  btn.addEventListener("click", () => {
    count++;
    btn.textContent = "Clicked " + count + " times";
  });
</script>

Output in the browser:

Each click runs the handler and updates the button text. The page does not reload.

The Event Object

Every handler receives an event object, usually named event or e. Its property target is the element on which the event happened. For keyboard events, event.key holds the key that was pressed, such as "a" or "Enter". For form events, event.preventDefault() stops the browser's normal action, such as sending the form, which later units use for validation. Handlers for the mouse can read the pointer position from event.clientX and event.clientY.

Common mistakes

  • Writing addEventListener("click", show()) so that show runs immediately instead of on a click.
  • Putting a script before the element exists, so the listener cannot be attached.
  • Using inline onclick attributes across a large page, which mixes behaviour with markup.
  • Forgetting that the same element can have several listeners, and removing the wrong one.

Key takeaways

  • An event is an occurrence on a page, and a listener waits for it.
  • Use addEventListener with the event name and a handler function.
  • The handler receives an event object with details of what happened.
  • Pass the function name without brackets so that it runs only when the event fires.

Practice questions

  1. Name two mouse events.
  2. Which method attaches a listener to an element?
  3. Why is onclick in the HTML tag discouraged?

Answers to the practice questions

  1. For example click and mouseover.
  2. addEventListener.
  3. It mixes behaviour with markup and allows only one handler for that event.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III