JavaScript Mouse Events (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

JavaScript mouse events

What are mouse events?

  • Mouse events allow a webpage to react when the user moves the cursor over or away from an element

  • These events are used in JavaScript to create interactivity on a webpage

  • The two required events to know are:

    • onmouseover

    • onmouseout

onmouseover event

  • onmouseover runs when the cursor moves over an element

  • It is used to trigger a change such as:

    • Swapping an image

    • Showing text

    • Highlighting an object

  • It creates interactivity because the webpage responds instantly to user movement

onmouseout event

  • onmouseout runs when the cursor moves away from an element

  • It is used to reverse or change an effect such as restoring an image or hiding text

  • It ensures the webpage returns to its original state when the cursor leaves the area

Typical scenarios

Scenario

Event used

What happens

Why it is interactive

Logo changes when hovered

onmouseover and onmouseout

The image swaps when the cursor moves over it and returns when the cursor leaves

The user sees an instant visual change based on their movement

Theme park ride reveals an opening date

onmouseover

Text appears when the cursor moves over the image

New information is displayed only when the user interacts

Product image highlights on hover

onmouseover

The image becomes brighter or larger when hovered

The element visually responds to the user’s action

Menu option shows a submenu

onmouseover

A submenu appears when the user moves over a navigation item

Navigation expands only when the user interacts

Image changes to an alternative picture

onmouseover and onmouseout

The picture switches to a second image then returns to the first

The element reacts to movement and resets when left

Text enlarges when hovered

onmouseover and onmouseout

Font size increases on hover and returns to normal when the cursor leaves

The user directly causes the change through cursor movement

Button changes colour when hovered

onmouseover and onmouseout

The background colour changes on hover and resets afterwards

The button provides visual feedback to the user

Example

<img src="fox1.png"
     onmouseover="this.src='fox2.png'"
     onmouseout="this.src='fox1.png'">
  • onmouseover swaps the image when the cursor moves over it

  • onmouseout restores the original image when the cursor leaves

Worked Example

The "Future Technologies" webpage includes a gallery of conceptual products. When the user hovers their mouse cursor directly over the image of a conceptual product, the image temporarily changes to a short video demonstration. When the cursor moves off the image, the display reverts back to the static image. This feature is implemented using mouse events to add interactivity to the page.

(i) Identify the specific JavaScript mouse event required to trigger the demonstration video when the cursor first moves onto the image

[1]

(ii) State the primary scripting language used to implement mouse event interactivity (onmouseover/onmouseout) on a webpage

[1]

Answers

  • (i) onmouseover [1 mark]

  • (ii) JavaScript [1 mark]

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.