Vue Event Modifiers
Event modifiers in Vue modify how events trigger the running of methods and help us handle events in a more efficient and straightforward way.
Event modifiers are used together with the Vue v-on
directive, to for example:
- Prevent the default submit behavior of HTML forms (
v-on:submit.prevent
) - Make sure that an event can only run once after the page is loaded (
v-on:click.once
) - Specify what keyboard key to use as an event to run a method (
v-on:keyup.enter
)
How To Modify The v-on
Directive
Event modifiers are used to define how to react on an event in more detail.
We use event modifiers by first connecting a tag to an event like we have seen before:
<button v-on:click="createAlert">Create alert</button>
Now, to define more specifically that the button click event should only fire one time after the page loads, we can add the .once
modifier, like this:
<button v-on:click.once="createAlert">Create alert</button>
Here is an example with the .once
modifier:
Example
The .once
modifier is used on the <button>
tag to only run the method the first time the 'click' event happens.
<div id="app">
<p>Click the button to create an alert:</p>
<button v-on:click.once="creteAlert">Create Alert</button>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
methods: {
createAlert() {
alert("Alert created from button click")
}
}
})
app.mount('#app')
</script>
Try it Yourself »
Note: It is also possible to handle an event inside the method instead of using event modifiers, but event modifiers make it a lot easier.
Different v-on
Modifiers
Event modifiers are used in different situations. We can use event modifiers when we listen to keyboard events, mouse click events, and we can even use event modifiers in combination with each other.
The event modifier .once
can be used after both keyboard and mouse click events.
Keyboard Key Event Modifiers
We have three different keyboard event types keydown
, keypress
, and keyup
.
With each key event type, we can specify exactly what key to listen to after a key event occurs. We have .space
, .enter
, .w
and .up
to name a few.
You can write the key event to the web page, or to the console with console.log(event.key)
, to find the value of a certain key yourself:
Example
The keydown
keyboard event triggers the 'getKey' method, and the value 'key' from the event object is written to the console and to the web page.
<input v-on:keydown="getKey">
<p> {{ keyValue }} </p>
data() {
return {
keyValue = ''
}
},
methods: {
getKey(evt) {
this.keyValue = evt.key
console.log(evt.key)
}
}
Try it Yourself »
We can also choose to limit the event to happen only when a mouse click or a key press happens in combination with system modifier keys .alt
, .ctrl
, .shift
or .meta
. The system modifier key .meta
represents the Windows key on Windows computers, or command key on Apple computers.
Key Modifier | Details |
---|---|
.[Vue key alias] |
The most common keys have their own aliases in Vue:
|
.[letter] |
Specify the letter that comes when you press the key. As an example: use the .s key modifier to listen to the 'S' key. |
.[system modifier key] |
.alt , .ctrl , .shift or .meta . These keys can be used in combination with other keys, or in combination with mouse clicks. |
Example
Use the .s
modifier to create an alert when the user writes an 's' inside the <textarea> tag.
<div id="app">
<p>Try pressing the 's' key:</p>
<textarea v-on:keyup.s="createAlert"></textarea>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
methods: {
createAlert() {
alert("You pressed the 'S' key!")
}
}
})
app.mount('#app')
</script>
Try it Yourself »
Combine Keyboard Event Modifiers
Event modifiers can also be used in combination with each other so that more than one thing must happen simultaneous for the method to be called.
Example
Use the .s
and .ctrl
modifiers in combination to create an alert when 's' and 'ctrl' are pressed simultaneously inside the <textarea>
tag.
<div id="app">
<p>Try pressing the 's' key:</p>
<textarea v-on:keydown.ctrl.s="createAlert"></textarea>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
methods: {
createAlert() {
alert("You pressed the 'S' and 'Ctrl' keys, in combination!")
}
}
})
app.mount('#app')
</script>
Try it Yourself »
Mouse Button Modifiers
To react on a mouse click, we can write v-on:click
, but to specify which mouse button that was clicked, we can use .left
, .center
or .right
modifiers.
Trackpad users: You might need to click with two fingers, or in the lower right hand side of the trackpad on your computer to create a right click.
Example
Change the background color when a user right-clicks in the <div>
element:
<div id="app">
<div v-on:click.right="changeColor"
v-bind:style="{backgroundColor:'hsl('+bgColor+',80%,80%)'}">
<p>Press right mouse button here.</p>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
data() {
return {
bgColor: 0
}
},
methods: {
changeColor() {
this.bgColor+=50
}
}
})
app.mount('#app')
</script>
Try it Yourself »
Mouse button events can also work in combination with a system modifier key.
Example
Change the background color when a user right-clicks in the <div>
element in combination with the 'ctrl' key:
<div id="app">
<div v-on:click.right.ctrl="changeColor"
v-bind:style="{backgroundColor:'hsl('+bgColor+',80%,80%)'}">
<p>Press right mouse button here.</p>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
data() {
return {
bgColor: 0
}
},
methods: {
changeColor() {
this.bgColor+=50
}
}
})
app.mount('#app')
</script>
Try it Yourself »
The event modifier .prevent
can be used in addition to the .right
modifier to prevent the default drop-down menu to appear when we right click.
Example
The drop-down menu is prevented from appearing when you right click to change the background color of the <div>
element:
<div id="app">
<div v-on:click.right.prevent="changeColor"
v-bind:style="{backgroundColor:'hsl('+bgColor+',80%,80%)'}">
<p>Press right mouse button here.</p>
</div>
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
data() {
return {
bgColor: 0
}
},
methods: {
changeColor() {
this.bgColor+=50
}
}
})
app.mount('#app')
</script>
Try it Yourself »
It would be possible to prevent the drop-down menu from appearing after right click by using event.preventDefault()
inside the method, but with the Vue .prevent
modifier the code becomes more readable and easier to maintain.
You can also react on left button mouse clicks in combination with other modifiers, like click.left.shift
:
Example
Hold the 'shift' keyboard key and press left mouse button on the <img>
tag to change image.
<div id="app">
<p>Hold 'Shift' key and press left mouse button:</p>
<img v-on:click.left.shift="changeImg" v-bind:src="imgUrl">
</div>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<script>
const app = Vue.createApp({
data() {
return {
imgUrlIndex: 0,
imgUrl: 'img_tiger_square.jpeg',
imgages: [
'img_tiger_square.jpeg',
'img_moose_square.jpeg',
'img_kangaroo_square.jpeg'
]
}
},
methods: {
changeImg() {
this.imgUrlIndex++
if(this.imgUrlIndex>=3){
this.imgUrlIndex=0
}
this.imgUrl = this.images[this.imgUrlIndex]
}
}
})
app.mount('#app')
</script>
Try it Yourself »