Event Handling in Vue.js

Event handling occupies a major part of any application. Vue.js as a framework provides very robust ways in which one can handle various events on the DOM. Similar to Angular, Vue.js also uses special directives that enable Vue.js to do event Handling. There are various events that Vue.js can handle but we will just concentrate on few of the most commonly used events. There are two ways in which we can trigger events and I will give you an example of both of them. We will use JSFiddle for the Vue Examples.

Prior to this I assume you Guys have an idea of Data Binding using Vue, there is special section for Dropdown as well.

These can be divided into three parts.

  1. Manipulating Properties using Events
  2. Triggering simple Events without arguments
  3. Passing Arguments To Methods while listening to events

Remember all the events in Vue.js can be triggered using a directive called v-on. This directive is pretty handy and can help seamlessly to attach events to it. So keep in mind that this particular directive will always go hand in hand whenever we talk about event handling. Hopefully you will also be interested in Global Event Bus. If you are do check it out.

Getting Started

To get started let’s have a look at the syntax used with the directive v-on. This is a very trivial example where we can manipulate certain properties on various Events that are available on the DOM. So let’s see the syntax of it in the HTML format.

In the above code, we have shown three different types of events and how they are being assigned to a specific method. This is the way in which we have to use the directive. Now let’s proceed with the three different cases which we talked about.

Checkout the entire Section on Vuejs Tutorials. We have some really easy content to get started with. Hope you guys will check it out!!

Manipulating Properties using Events

In this particular sample, we will just go ahead and attach a JavaScript expression within a data property and see it change. This is nothing but Script based Evaluation on the DOM based event. Have a look at the code as well as the sample below.

HTML

Vue.js Code

Output

You can see the sample below on JS Fiddle Results section.

Though this does not solve many problems, it does help us a lot in solving few of the issues. you can have it in places where you directly want to manipulate the property. This is handy in such situations. Now let’s have a look at events that trigger methods of a component.

Trigger Methods with and without Arguments

In order to keep the tutorial small, we will try to cover both the topics together. In this current situation, we will see how to trigger methods that are a part of the component. We will take up the similar example and expand it to do the same. You will be able to see the completed code in the JSFiddle sample. Below goes the HTML and the JavaScript code.

HTML

Vue.js Code

Now if you have a look at the code, you will see the simple method declaration. These methods don’t carry any argument. These, when triggered, change the inner HTML of the h1 tag. The different events we covered out here are as follows:-

  1. Click
  2. Double Click
  3. Mouse Over

These are just few sample of the events that can be handled. Have a look at the below GIF to see this in action.

Trigger Events in Vue.js, Functions without Arguments

Trigger Events in Vue.js, Functions without Arguments

Passing Data Through method Arguments

Here we will add an extra button and carry data from Input box and then pass it to the argument changing some text on the DOM. The code is pretty simple and goes as mentioned below:-

HTML

Here you can see that the getData() function takes an argument called data which is the data bound to input. This data gets passed to the function there by changing the DOM.

Vue.js Code

We will add an additional method which takes in an argument. That’s it!! The code below Concatenates the message New Data Point is to the text in the HTML and reassigns the data. You can see how I passed a string from the getData method.

You can see this working either on the JSFiddle or the GIF below.

Methods with Arguments Vue.js

Methods with Arguments Vue.js

You can have a look at the entire code out here on JSFiddle.

Conclusion

Let’s sum up the whole thing by seeing the entire code together in the JSFiddle. In the above sample, we have taken care of three kinds of ways in which we tackle events. Each one of them has a specific use case and is a must know for any Vue.js Developer. Please feel free to put in your comments and please let us know if we can make changes to our tutorials and make them more engaging. In the above paragraph, you can see the Entire code being projected using JSFiddle.

NOTE : Post has been updated according to some suggestions provided by our reader @froller


Also published on Medium.

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Pin It on Pinterest

Share This