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).
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
| Category | Events | Typical use |
|---|---|---|
| UI events | load, resize, scroll | Run set-up code when the page has loaded (window load or DOMContentLoaded); adjust a layout when the window is resized |
| Keyboard | keydown, keyup | Respond to key presses; keyup fires when the key is released, after the value has changed (live character counters, search as you type) |
| Mouse | click, dblclick, mouseover, mouseout, mousedown, mouseup, mousemove | Buttons, rollover images, drag effects |
| Focus and blur | focus, blur | focus fires when an element is selected for input; blur when it loses focus (show a hint on focus, check a field on blur) |
| Form | submit, input, change, reset | Validate 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 requiredA 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?
- The count must update on each key, after the value has changed: use keyup (or input).
- 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; }); ```
Check yourself
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.)
- 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.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Information Technology specification (first teaching 2018), Unit 2 Topics 7 to 11
- Pearson qualifications: Edexcel International Advanced Level Information Technology (2018)
- Pearson WIT12/01 mark scheme, June 2024 series
- Pearson WIT12/01 question paper, October 2021 series
- Pearson WIT12/01 examiner report, June 2025 series
Last updated 8 November 2026. Spotted a mistake? Tell us.