Vue.js Listening to Events
Last Updated :
23 Jul, 2025
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
Advanced JavaScript Backend Basics Getting Started with JavaScript Backend Basics The following article is a continuation to my previous article. In this article, we will dive deep into some of the advanced JavaScript concepts that are being used widely in the industry. "==" vs "===" The "==" coerces the types of the input that is it
5 min read
What is backend Developer Backend developers are a crucial part of modern web development teams, responsible for building the server-side logic and infrastructure that powers web applications. In this comprehensive article, we'll see the role of a backend developer, covering various aspects such as responsibilities, skills r
5 min read
What is Backend Development? Skills, Salary and Roles Backend development is a blessing to all of us that we are able to get everything done by just sitting at home. Be it booking tickets, watching movies, or any sort of thing, backend development plays a major role in building an application. It is also one of the highly demanding professions in the I
7 min read
Advantages and Disadvantages of JavaScript JavaScript is an interpreted programming as well as a scripting language. Many of these are related to the way, JavaScript is often executed directly in a client's browser commonly utilized in web development. It was originally developed by Netscape as a way to feature dynamic and interactive elemen
3 min read
Difference between Node.js and JavaScript JavaScript and Node.js are both crucial in modern web development, but they serve different purposes and are used in different environments. JavaScript is a programming language primarily used for client-side web development, while Node is a runtime environment that allows JavaScript to be executed
3 min read
How to Use Node.js for Backend Web Development? In the world of website design nowadays, NodeJS is a supportive tool for making capable backend systems. Whether you are an experienced web designer or just beginning out, NodeJS can upgrade your aptitude and help you in building extraordinary websites. This guide will show you how to use NodeJS to
8 min read
Top 10 Backend Technologies You Must Know To provide any responsive and effective software solution, frontend, and backend are the two most essential technologies that work together. A back-end framework is used to create server-side web architectures stably and efficiently. Backend technologies focus on improving the hidden aspects of the
11 min read
10 Best Backend JavaScript Frameworks in 2025 When JavaScript was first created, it used to run only in the browser of the client (user). At that time, it was sufficient, as websites used to be static and just displayed some information for the user to see and read. But as both time and technology moved forward, web development changed. Soon, t
11 min read
Good Coding Practices For Backend Developers API, Authentication, Design Patterns, MVC, Cache, Cookies, Sessions, JSON, Endpoints, Server, Hosting, Postman, CRUD, Curl...Aren't all these words familiar to you???If yes, then surely you're a backend developer or working on the backend part of the application. Clients often underestimate the work
9 min read
12 Backend Development Tools For Web Developers While Frontend Web Development is concerned with the designing of the user interface of the website using web technologies like HTML, CSS, JavaScript, etc. - Backend Web Development (or you can say Server-Side Development) is responsible for the appropriate functioning of the website.In simple words
9 min read