Sometimes you need to know exactly what happened when a user interacts with your page. Accessing the native event object lets you see details like which key was pressed or where the mouse clicked.
When you want to know which key a user pressed in an input box.
When you need to find the mouse position during a click.
When you want to prevent the default browser action on a button click.
When you want to check if a modifier key like Shift or Ctrl was held during an event.
Syntax
Vue
<template>
<button @click="handleClick">Click me</button>
</template>
<script setup>
function handleClick(event) {
// event is the native event object
}
</script>
The event object is automatically passed as the first argument to your event handler.
You can access properties like event.target, event.type, event.preventDefault(), and more.
Examples
This example logs the key the user pressed inside an input box.
This example logs the mouse position inside a div as you move the mouse.
Vue
<template>
<div @mousemove="onMouseMove">Move your mouse here</div>
</template>
<script setup>
function onMouseMove(event) {
console.log(`Mouse at: (${event.clientX}, ${event.clientY})`);
}
</script>
Sample Program
This Vue component shows an input box. When you press a key, it updates the text below to show which key was pressed by accessing the native event object.
Hint: Always receive event as parameter to use event methods [OK]
Common Mistakes:
Assuming event is globally available
Confusing template modifiers with method code
Using preventDefault without event parameter
5. You want to log the mouse coordinates when a user clicks a div in Vue. Which code correctly accesses the native event object and logs the coordinates?
hard
A. <div @click="logCoords">Click</div>\n<script setup>\nfunction logCoords(event) {\n console.log(event.clientX, event.clientY);\n}\n</script>
B. <div @click="logCoords($event)">Click</div>\n<script setup>\nfunction logCoords() {\n console.log(event.clientX, event.clientY);\n}\n</script>
C. <div @click="logCoords($event)">Click</div>\n<script setup>\nfunction logCoords(event) {\n console.log(event.pageX, event.pageY);\n}\n</script>
D. <div @click="logCoords">Click</div>\n<script setup>\nfunction logCoords() {\n console.log($event.clientX, $event.clientY);\n}\n</script>
Solution
Step 1: Confirm event object passing in template
To access the native event object in the method, you must pass $event explicitly in the template.
Step 2: Check method parameter and properties used
The method must accept event as a parameter and use valid mouse coordinate properties like pageX and pageY or clientX and clientY. <div @click="logCoords($event)">Click</div>\n<script setup>\nfunction logCoords(event) {\n console.log(event.pageX, event.pageY);\n}\n</script> correctly passes $event and uses pageX, pageY.
Final Answer:
Option C passes $event and logs pageX, pageY correctly -> Option C
Quick Check:
Pass $event and accept event param = C [OK]
Hint: Pass $event in template and accept event param in method [OK]
Common Mistakes:
Not passing $event in template
Using event without parameter
Trying to use $event inside script without passing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.