Vue.js Listening to Events
Last Updated :
25 Jul, 2024
Vue.js provides the different events that are used to handle user interactions and component communication. To listen to those DOM events, Vue.js provides a v-on directive. We can use the shortened @ symbol in place of the v-on directive. Listening to Events is the programming concept of waiting for an event and in response executing some instruction.
Syntax
<tagname v-on:event="handler" >
// Tag data
</tagname>
Or
<tagname @event="handler">
// Tag data
</tagname>
Properties
- event: It is an event to which we are listening and trigger our desired instruction when an event occurs.
- handler: It is the way in which we handle the occurred event.
The value of the handler can be of the following types:
- Inline Handlers: As the name suggests, inline handlers are used in cases where we only want to execute single-line code.
The syntax for the inline handler can be as:
<button v-on:click='x++'>
// Given inline handler increase the value of x by 1;
Increase
< /button>
- Method Handlers: Method handler is used in case we have more lines of instruction, than inline handler and have some raw data to pass to the event handler for the progress of the handler.
<button v-on:click="submit('sam')">
Sumbit
</button>
The above code triggers the submit handler with the value 'sam' on the click event on the button marked as Submit. The handler in response does some work to handle the event.
Example 1: In this example, we will trigger an event on mouse over and mouse click event and handle this event using the v-on directive and @ symbol.
HTML
<!-- App.vue -->
<template>
<h1 v-on:mouseover="over">
GeeksforGeeks
</h1>
<h2 v-on:mouseover="over">
Method Handler
</h2>
<h2 @on:mouseover="over">
Name: {{ name }}
</h2>
<h2 @on:mouseover="over">
Course: {{ Course }}
</h2>
<h2 @on:mouseover="over">
Age: {{ Age }}
</h2>
<h2 v-on:mouseover="over">
Batch: {{ Batch }}
</h2>
<button v-on:click="Pname">
Print Name
</button>
<button v-on:click="Pbatch">
Print Batch
</button><br />
<h2 id="temp"></h2>
</template>
<script>
export default {
name: 'App',
data() {
return { name: 'Sam',
Course: 'Mtech',
Age: '23',
Batch: '5th' };
},
methods: {
Pname: function () {
alert('Name is ' + this.name)
},
Pcourse: function () {
alert('Course is ' + this.Course)
},
Pbatch: function () {
alert('Batch is ' + this.Batch)
},
over: function (event) {
document.getElementById('temp').innerHTML =
(event.target).innerHTML;
}
}
}
</script>
<style>
#app {
text-align: center;
margin-top: 60px;
}
h1 {
color: rgb(40, 212, 103)
}
</style>
Output:
Example 2: In this example, we will trigger an event on keyup.enter, keyup.delete, click, and handle this event using v-on directive and @ symbol.
HTML
<!-- App.vue -->
<template>
<div align="center">
<h1>GeeksforGeeks</h1>
<h1>
Key Modifiers
</h1>
Name:
<input v-on:keyup.enter="NameEnt"
@click="N1"
v-on:keyup.delete="NameDel"
placeholder="Enter Name" />
<br />
Course:
<input v-on:keyup.enter="CourseEnt"
@click="C1"
v-on:keyup.delete="CourseDel"
placeholder="Enter Course" /> <br />
Age:
<input v-on:keyup.enter="AgeEnt"
@click='A1'
v-on:keyup.delete="AgeDel"
placeholder="Enter Age" /> <br />
<button v-on:click="submit">
Submit
</button>
<h3>
{{name}} <br />
{{ Course }} <br />
{{ Age }}
</h3>
</div>
</template>
<script>
export default {
name: "App",
data() {
return {
name: '',
Course: '',
Age: ''
};
},
methods: {
submit: function () {
alert('Form Submited')
},
N1: function () {
this.name = 'Typing Name...';
},
C1: function () {
this.Course = 'Typing Course...';
},
A1: function () {
this.Age = 'Typing Age...';
},
NameEnt: function (event) {
this.name =
'Entered Name is : ' + event.target.value
},
NameDel: function () {
this.name =
'Entered Name Deleted'
},
CourseEnt: function (event) {
this.Course =
'Entered Course is :' + event.target.value
},
CourseDel: function () {
this.Course = 'Entered Course is Delete'
},
AgeEnt: function (event) {
this.Age = 'Entered Age is :' + event.target.value
},
AgeDel: function () {
this.Age = "Entered Age is Deleted"
}
}
};
</script>
<style>
h1 {
color: green;
}
</style>
Output:
Reference: https://vuejs.org/guide/essentials/event-handling.html#listening-to-events
Similar Reads
Node.js Events An event in NodeJS is an action or occurrence, such as a user click, a file being read, or a message being received, that NodeJS can respond to. Events are managed using the EventEmitter class, which is part of NodeJS's built-in events module. This allows NodeJS to react to various actions asynchron
7 min read
Vue.js Event Handling Vue.js is an open-source ModelâViewâViewModel front-end JavaScript framework for building user interfaces and single-page applications. Vue.js has many own directives for DOM manipulation such as v-bind, v-on, v-model, etc. In this article, we will see how to handle the events and their Implementati
11 min read
Vue.js Reusing Components Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
4 min read
Vue.js Event Modifiers Vue.js is a progressive javascript framework for developing web user interfaces. It is a performant, approachable, and versatile framework. We can create Single Page Applications as well as Full Stack applications. It is built on top of HTML, CSS, and Javascript which makes it easier for developers
3 min read
Vue.js | v-text directive The v-text directive is a Vue.js directive used to update a elementâs textContent with our data. It is just a nice alternative to Mustache syntax. First, we will create a div element with id as app and let's apply the v-text directive to this element with data as a message. Now we will create this m
1 min read