loading

Vue v-on

The v-on directive in Vue instructs the browser, much like event handling in standard JavaScript:

  1. which event—such as “click,” “keydown,” “mouseover,” etc.—to pay attention to
  2. How to handle that situation if it arises

Examples using v-on

To show how v-on may be used with various events and different code to run when these events occur, let’s look at a few samples.

Let’s look at a few examples to demonstrate how v-on may be used with different events and different code to run when these events occur.

onclick Event

We can take action in response to predefined events with the v-on directive.

To take action when the element is clicked, use v-on:click.

Example

The <button> element uses the v-on directive to listen for the ‘click’ event. The yellow <div> behind the lightbulb is either visible or hidden depending on whether the ‘click’ event causes the ‘lightOn’ data property to be toggled between ‘true’ and ‘false’.

				
					<div id="app">
  <div id="lightDiv">
    <div v-show="lightOn"></div>
    <img decoding="async" src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%200%200'%3E%3C/svg%3E" alt="Vue V-On -" title="Vue V-On 1" data-lazy-src="img_lightBulb.svg"><noscript><img decoding="async" src="img_lightBulb.svg" alt="Vue V-On -" title="Vue V-On 1"></noscript>
  </div>
  <button v-on:click="lightOn = !lightOn">Switch light</button>
</div>

<script data-minify="1" src="https://codingask.com/wp-content/cache/min/1/vue@3/dist/vue.global.js?ver=1730183021" defer></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        lightOn: false
      }
    }
  })
  app.mount('#app')
</script>
				
			

oninput Event

To take action when an element receives input—such as a keystroke inside a text field—use v-on:input.

Example

Determine how many keystrokes were made in an input text field:

				
					<div id="app">
  <input v-on:input="inpCount++">
  <p>{{ 'Input events occured: ' + inpCount }}</p>
</div>

<script data-minify="1" src="https://codingask.com/wp-content/cache/min/1/vue@3/dist/vue.global.js?ver=1730183021" defer></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        inpCount: 0
      }
    }
  })
  app.mount('#app')
</script>
				
			

mousemove Event

To take action when the mouse pointer passes over an element, use v-on:mousemove.

Example

Whenever the mouse pointer passes over a <div> element, its background color can be changed:

				
					<div id="app">
  <p>Move the mouse pointer over the box below</p>
  <div v-on:mousemove="colorVal=Math.floor(Math.random()*360)"
       v-bind:style="{backgroundColor:'hsl('+colorVal+',80%,80%)'}">
  </div>
</div>

<script data-minify="1" src="https://codingask.com/wp-content/cache/min/1/vue@3/dist/vue.global.js?ver=1730183021" defer></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        colorVal: 50
      }
    }
  })
  app.mount('#app')
</script>
				
			

Use v-on in a v-for Loop

The v-on directive can also be utilized within a v-for loop.

Every iteration within the v-on value has access to the array’s items.

Example

Show a list based on the food array, and then add a click event to each item such that it can modify the image’s source by using a value from the array item.

				
					<div id="app">
  <img v-bind:src="imgUrl">
  <ol>
    <li v-for="food in manyFoods" v-on:click="imgUrl=food.url">
      {{ food.name }}
    </li>
  </ol>
</div>

<script data-minify="1" src="https://codingask.com/wp-content/cache/min/1/vue@3/dist/vue.global.js?ver=1730183021" defer></script>
<script>
  const app = Vue.createApp({
    data() {
      return {
        imgUrl: 'img_salad.svg',
        manyFoods: [
          {name: 'Burrito', url: 'img_burrito.svg'},
          {name: 'Salad', url: 'img_salad.svg'},
          {name: 'Cake', url: 'img_cake.svg'},
          {name: 'Soup', url: 'img_soup.svg'}
        ]
      }
    }
  })
  app.mount('#app')
</script>
				
			

Shorthand for v-on

@‘ is the shorthand for ‘v-on‘.

Example

Here, ‘@‘ is used in place of ‘v-on‘:

				
					<button @:click="lightOn = !lightOn">Switch light</button>
				
			

A bit later in this course, we will begin using the @ syntax.

Share this Doc

Vue v-on

Or copy link

Explore Topic