All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Events: UI, keyboard, mouse, focus, blur and form events

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

Events are things that happen on the page, such as a click or a key press. An event handler is a function that runs when the event happens (spec 9.3.3).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

Attaching an event handler

const btn = document.getElementById("go");
btn.addEventListener("click", showPrice);    // pass the function, do not call it
function showPrice() {
  document.getElementById("out").textContent = "Price: 5.00";
}
// or an anonymous function
btn.addEventListener("click", function (event) {
  console.log("Clicked " + event.target.id);
});

addEventListener(event, function) is preferred. The older ways are an HTML attribute (<button onclick="showPrice()">) or a property (btn.onclick = showPrice;). Write showPrice, not showPrice(), when attaching: with brackets the function would run straight away. The event object (event or e) tells the handler what happened: event.target is the element, event.key is the key pressed.

Event types

CategoryEventsTypical use
UI eventsload, resize, scrollRun set-up code when the page has loaded (window load or DOMContentLoaded); adjust a layout when the window is resized
Keyboardkeydown, keyupRespond to key presses; keyup fires when the key is released, after the value has changed (live character counters, search as you type)
Mouseclick, dblclick, mouseover, mouseout, mousedown, mouseup, mousemoveButtons, rollover images, drag effects
Focus and blurfocus, blurfocus fires when an element is selected for input; blur when it loses focus (show a hint on focus, check a field on blur)
Formsubmit, input, change, resetValidate on submit; input fires on every change to a value; change fires when a select, checkbox or edited box is committed

Events in action

const box = document.getElementById("name");
const hint = document.getElementById("hint");
box.addEventListener("focus", function () {
  hint.textContent = "Enter your full name";
});
box.addEventListener("blur", function () {
  if (box.value === "") {
    hint.textContent = "Name is required";
  } else {
    hint.textContent = "";
  }
});

Output after the user clicks into the empty box and then leaves it:

Enter your full name
Name is required

A form's submit event handler can call event.preventDefault() to stop the form being sent when validation fails (next lessons).

Worked example

A text area with id msg must show how many characters have been typed, in a span with id count, updating as the user types. Which event and code should be used?

  1. The count must update on each key, after the value has changed: use keyup (or input).
  2. Read msg.value.length and write it into the span.

Answer: ```js const msg = document.getElementById("msg"); msg.addEventListener("keyup", function () { document.getElementById("count").textContent = msg.value.length; }); ```

Key idea: An event handler is a function that runs when an event happens; attach it with addEventListener("event", functionName). UI: load, resize, scroll; keyboard: keydown, keyup; mouse: click, mouseover, mouseout; focus and blur; form: submit, input, change.

Check yourself

  1. The user clicks the button three times. What does the paragraph show? ```html <button id="btn">Press</button> <p id="out"></p> ``` ```js let clicks = 0; const b = document.getElementById("btn"); b.addEventListener("click", function () { clicks++; document.getElementById("out").textContent = "Clicked " + clicks + " times"; }); ```
    Show answer

    Clicked 3 times. Each click adds 1, so after three clicks the paragraph shows Clicked 3 times.

  2. Which event fires when the user clicks or tabs into a text box?
    • blur
    • focus
    • submit
    • load
    Show answer

    focus. focus fires when an element becomes active for input; blur when it stops being active.

  3. A character counter must update after each key is released. Which event should it use?
    • load
    • submit
    • mouseover
    • keyup
    Show answer

    keyup. keyup fires when a key is released, after the text has changed.

  4. What is wrong with `btn.addEventListener("click", check());`?
    • check() runs immediately and its return value is attached instead of the function
    • click must be written in capitals
    • addEventListener only works on links
    • The function must be called handler
    Show answer

    check() runs immediately and its return value is attached instead of the function. Pass the function itself (check). With brackets it is called straight away.

  5. Give one example of an event and explain the purpose of an event handler.
    Show answer

    A click on a button is an event; the event handler is the function that runs in response to it, for example to check the form.. Example events: click, keyup, load, submit, focus. An event handler is the function that is run when that event occurs.

  6. The user moves the pointer over the image and then away from it. Which image file path was set last, so that it is the picture shown at the end? Type the path exactly as it appears in the code. ```html <img id="pic" src="images/grey.png" alt="Pool"> ``` ```js const pic = document.getElementById("pic"); pic.addEventListener("mouseover", function () { pic.src = "images/colour.png"; }); pic.addEventListener("mouseout", function () { pic.src = "images/grey.png"; }); ```
    Show answer

    images/grey.png. mouseover set the colour picture and mouseout set it back, so the image shown is images/grey.png. (Reading pic.src in a browser gives the full address, ending in this path.)

  7. Which event is used to run set-up code, such as displaying the first slide, as soon as the page has loaded?
    • blur on the body
    • keydown on the document
    • load on the window (or DOMContentLoaded on the document)
    • submit on the window
    Show answer

    load on the window (or DOMContentLoaded on the document). The load event (or DOMContentLoaded) fires when the page is ready. Pearson's 2025 report noted candidates who did not call their set-up function on page load.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.