Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

onmousemove — handling mouse movement

The global HTML attribute onmousemove contains JavaScript that handles the mousemove event. The example dispatches it programmatically so the result does not depend on mouse movement.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

onmousemove="JavaScript statements"

onmousemove is a global HTML attribute associated with the mousemove event, used for mouse movement. Its value contains JavaScript statements, not just a function name.

Inside the attribute code, event represents the event and this refers to the handling element. The corresponding JavaScript property element.onmousemove accepts a function or null; assigning a function replaces that property's previous handler.

Content Security Policy can block attribute-based handlers. Never insert untrusted text there: it is executable code.

The example dispatches a synthetic event and sets the element's text to mousemove. It checks event handling, not physical mouse movement.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>onmousemove demo</title>
<div id="area" onmousemove="this.textContent = event.type">Move here</div>
<script>
  const area = document.getElementById('area');
  area.dispatchEvent(new MouseEvent('mousemove'));
</script>
</html>

Result

mousemove

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources