Web Design & Development · JavaScript

WDD11 - JavaScript: events and functions

Thu 3 Dec 2026 · P2/P3 (double)
Content scoped for a double period · matches available time
Learning intentions
Success criteria
Warm up - CSS hover and descendant selectors recap

WDD11 is the course's first pure JavaScript lesson - the warm up bridges from WDD10's CSS :hover, which JavaScript events can do more than.

WU1
Which CSS pseudo-class applies a style only while the mouse pointer is over an element?
 
WU2
Write a descendant selector that targets only <a> elements inside <nav>.
 
WU3
Which display value must be set on a link so its whole padded box, not just the text, is clickable?
  • inline is the default for a - it does not expand the clickable area.
  • Correct. display: block makes the full padded box clickable.
  • none removes the element from the page entirely.
  • float positions an element - it does not affect the clickable area on its own.

Key vocabulary

JavaScript
A programming language that runs in the browser, adding interactivity and behaviour beyond what HTML and CSS alone can do.
<script> element
The HTML element that JavaScript code is written inside, or that links to an external JavaScript file.
Function
A named block of JavaScript statements that does nothing on its own until it is called.
Event
Something that happens to an element that JavaScript can respond to, such as a click or a mouse movement.
Event handler
The code, attached to an element, that runs when a particular event occurs on it - here, an HTML attribute calling a function.
onclick
An event attribute that runs its function when the element is clicked.
onmouseover
An event attribute that runs its function when the mouse pointer moves onto the element.
onmouseout
An event attribute that runs its function when the mouse pointer moves off the element.

Introducing JavaScript

A third language, with a different job

Every lesson so far has used two languages: HTML for structure and content, and CSS for appearance. JavaScript is the third language in the course, and it has a different job from both: it adds behaviour - code that runs in response to something happening, such as a pupil clicking a button or moving their mouse over an image. JavaScript code is written inside a <script> element, which can appear in the <head> or the <body> of an HTML page.

Functions: named blocks of code that wait to be called

The basic building block of JavaScript behaviour is the function: a named block of statements, written using the function keyword, that sits inactive until something calls it by name. Writing a function does not run it - a function's code only executes at the moment it is called, which in this lesson happens through an event.

function showMessage() {
  alert('Button was clicked!');
}

On its own, this function does nothing at all when the page loads. It only runs once something calls showMessage() - which is exactly what an event attribute is for.

Events and event handlers

Three mouse events

An event is something that happens to an element - a click, or the mouse pointer moving onto or off it. This lesson covers three: onclick (the element is clicked), onmouseover (the mouse pointer moves onto the element), and onmouseout (the mouse pointer moves off the element). Each is written as an HTML attribute on the element itself, with the function to call - including its parentheses - as the attribute's value.

<button onclick="showMessage()">Click me</button>

onmouseover and onmouseout are usually a pair

Unlike onclick, which fires once per click and needs no partner, onmouseover and onmouseout are almost always used together. onmouseover makes some change happen when the pointer arrives; without a matching onmouseout to reverse that change when the pointer leaves, the changed appearance or state would simply stay changed forever, rather than behaving like a genuine hover effect.

onclick

Fires once per click. No partner event needed.

onmouseover

Fires when the pointer arrives. Usually pairs with onmouseout.

onmouseout

Fires when the pointer leaves. Reverses whatever onmouseover changed.

Same visual result, different tool changing a box's colour on hover
CSS :hover (WDD10) Style only, same element only
JS onmouseover/onmouseout Style, text, or any other element

Worked examples

Example 1 - onclick calling a function
<button onclick="showMessage()">Click me</button>
<p id="output"></p>

<script>
function showMessage() {
  document.getElementById('output').textContent = 'Button was clicked!';
}
</script>
1
The button's onclick attribute names the function to call, with parentheses: showMessage().
2
When clicked, the browser runs the matching function showMessage() { ... } defined in the script element.
The function finds the paragraph by its id and changes its text - nothing happens until the click occurs.
Example 2 - onmouseover and onmouseout as a pair
<div id="box" onmouseover="highlightBox()" onmouseout="resetBox()">
  Hover over me
</div>

<script>
function highlightBox() {
  document.getElementById('box').style.background = '#185FA5';
}
function resetBox() {
  document.getElementById('box').style.background = '#E6F1FB';
}
</script>
1
onmouseover calls highlightBox() the moment the pointer arrives, changing the background.
2
onmouseout calls resetBox() the moment the pointer leaves, changing the background back.
Without resetBox() and onmouseout, the box would stay highlighted permanently after the first hover.
Hover over me
Example 3 - What happens if the parentheses are missing
onclick="showMessage"

The browser treats this as a reference to the function, not a call to run it. Clicking the button does nothing visible.

onclick="showMessage()"

The parentheses actually call the function. Clicking the button runs its code as intended.

Always include the parentheses when calling a function from an event attribute, even though the function itself is defined with parentheses too.
Now you try

A pupil writes <div onmouseover="turnBlue()"> but does not add an onmouseout attribute at all. What will the box look like after the pointer has moved onto it once, then away again?

turnBlue() ran when the pointer arrived, so the box turned blue - but nothing was ever written to change it back, because there is no onmouseout attribute at all.

The box stays blue permanently, even after the pointer moves away.

Practical: events and functions, WebStorm

Build this from scratch - no starter file this time.

  1. In WebStorm, create a new HTML file called events.html.
  2. Add a button with an onclick attribute, and a paragraph with an id for it to update.
  3. Add a <script> element containing a function that changes the paragraph's text when the button is clicked.
  4. Add a separate div with onmouseover and onmouseout attributes, and matching functions that change its background colour and then change it back.
  5. Save, open in a browser, and check all three events (onclick, onmouseover, onmouseout) behave correctly.
<button onclick="showMessage()">Click me</button>
<p id="output"></p>

<div id="box" onmouseover="highlightBox()" onmouseout="resetBox()">
  Hover over me
</div>
<script>
function showMessage() {
  document.getElementById('output').textContent = 'Button was clicked!';
}
function highlightBox() {
  document.getElementById('box').style.background = '#185FA5';
}
function resetBox() {
  document.getElementById('box').style.background = '#E6F1FB';
}
</script>

Compare your own file against this rather than copying it - the exact ids and colours do not matter, but each event attribute should call a function with parentheses, and onmouseover should have a matching onmouseout.

Common mistakes
Exam tip

Use the exact event names onclick, onmouseover and onmouseout - not vaguer descriptions like "when hovered." If a question shows event-and-function code with something not working, check the parentheses on the function call and whether onmouseover has a matching onmouseout before looking for anything more complicated.

Task Set A - Core questions

Task Set A - Core questions
Complete all questions. Written and code answers reveal a model answer for self-assessment.
A1
Which HTML event attribute runs a function when the user clicks an element?
 
A2
Which event attribute runs a function when the mouse pointer moves onto an element?
 
A3
Which event attribute runs a function when the mouse pointer moves off an element?
 
A4
A button is written as <button onclick="showMessage">Click me</button> - no parentheses after showMessage. What happens when it is clicked?
  • Without parentheses, the function is referenced, not called.
  • Correct.
  • This does not cause a page-load error - it simply fails to call the function on click.
  • It does not run at all without the parentheses.
A5
Which keyword is used to declare a JavaScript function?
 
A6
Where must a JavaScript function's code be written before an event attribute can call it?
  • <style> is for CSS, not JavaScript.
  • Correct.
  • The function is defined in a script element; the attribute only calls it.
  • A function must be defined somewhere before it can be called.
A7
Explain why a nav bar link might use both onmouseover and onmouseout together, rather than onmouseover alone.
Model answer
A8
Which of these correctly calls a function named highlight from an onclick attribute?
  • This references the function without calling it.
  • Correct. The parentheses call the function.
  • This is a function declaration, not a call - and is not valid here.
  • call is not valid JavaScript syntax for this.
A9
This code is meant to highlight a box on hover and remove the highlight when the pointer leaves, but the box stays highlighted permanently after the first hover. Find the bug and write the corrected HTML/JavaScript.
<div id="box" onmouseover="highlightBox()">Hover over me</div>

<script>
function highlightBox() {
  document.getElementById('box').style.background = '#185FA5';
}
function resetBox() {
  document.getElementById('box').style.background = '#E6F1FB';
}
</script>
<div id="box" onmouseover="highlightBox()" onmouseout="resetBox()">Hover over me</div>

resetBox() was already written correctly, but nothing was calling it - the div was missing its onmouseout="resetBox()" attribute entirely, so the highlight from onmouseover was never reversed.

Task Set B - Extension

Task Set B - Extension
Beyond the core specification - written and practical answers are self-assessed against a model answer.
B1
Compare using JavaScript's onmouseover/onmouseout with using CSS's :hover (from WDD10) to change an element's appearance. When would a pupil choose JavaScript instead of CSS :hover?
Model answer
B2
Explain what happens if the function name given in an onclick attribute does not match any function actually defined in the script element.
Model answer
B3 - Practical (WebStorm, from scratch)
Extend today's practical file with a click counter - a genuinely new behaviour, not just a colour change.
  1. In your events.html file from the main practical, add a new paragraph with an id, starting with the text "Clicks: 0".
  2. Add a new button with an onclick attribute calling a function such as countClick().
  3. In your script, keep a variable outside any function to store the current count, starting at 0.
  4. Inside countClick(), increase the variable by 1 and update the paragraph's text to show the new count.
  5. Save, open in a browser, and click the button several times to check the count increases correctly each time.
<button onclick="countClick()">Click to count</button>
<p id="counter">Clicks: 0</p>
<script>
let clickCount = 0;

function countClick() {
  clickCount = clickCount + 1;
  document.getElementById('counter').textContent = 'Clicks: ' + clickCount;
}
</script>

The variable clickCount is declared outside the function so it keeps its value between clicks - a variable declared inside the function would reset to 0 every time it ran, and the counter would never go above 1.

File this in OneNote under:
Higher Computing Science > Web Design & Development > WDD11

Teacher notes - Shift+T to toggle

Timing: content is scoped for a double period and lands on one (Thu 3 Dec 2026, per WDD.md §2a/§4 table) - no compression or extra-time adjustment needed, unlike the surrounding WDD8-10 run. Suggested pace: 8 min warm up + JS/CSS bridge, 15 min JavaScript/function intro + Example 1, 15 min events/pairing notes + context box + Example 2 (including the live hover demo), 10 min Example 3 + common mistakes, remaining time on Task Set A, then Task Set B and the WebStorm practical.

Scope control: deliberately keeps functions simple, with no parameters - matching Chris's L11 and the SQA spec's phrasing of "functions related to mouse events" as one combined, introductory skill. Parameter passing is already covered from the SDD side (SDD8); this lesson does not need to duplicate it. B3's click counter is the only place a variable persisting between calls is introduced, and is extension-only for that reason.

The parentheses-on-call mistake is the single highest-value bug to drill for exam purposes - it is easy to miss when reading code quickly, and A4/A8/A9 test it from three angles: conceptual MC, correct-syntax MC, and a debugging fix.

Practical built from scratch (no starter file), continuing the WDD6/WDD8/WDD10 (starter) vs WDD7/WDD9/WDD11 (scratch) alternation.