Event Handling — Vue

@click="add" (short for v-on:click) runs the function add when the element is clicked. The value can be a function name, or a short statement like count++…

On the counter at Mrs. Sri's stall there's a bell. The bell does nothing by itself. What makes it useful is Mrs. Sri's rule: "when the bell rings, I come to the cash register".

A button on a page is the same. Without a rule, you can click it many times and nothing happens. Event handling means telling Vue what to run when the user does something: clicks, types, presses a key, or sends a form. Something that happens on the page, such as a click, is called an event. In Vue, the bell is the button, the ring is the click, and Mrs. Sri's rule is written @click="add": when there is a click, run the function add.

@click: run code on a click

Here is an order counter for Iced Tea with two buttons:

const count = ref(0)

function add() {
  count.value++
}

---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">Add</button>
<button @click="count = 0">Cancel</button>
Event count Screen
Page appears 0 Iced Tea × 0
Click Add 1 Iced Tea × 1
Click Add 2 Iced Tea × 2
Click Cancel 0 Iced Tea × 0

With or without parentheses

This time add has one parameter, name, and puts it into the array orders. Two buttons call add in different ways:

const orders = ref([])

function add(name) {
  orders.value.push(name)
}

---template---
<button @click="add('Iced Tea')">Add Iced Tea</button>
<button @click="add">No parentheses</button>
<p>Orders: {{ orders.join(", ") }}</p>
Written The name that add receives "Orders:" after 1 click
@click="add('Iced Tea')" "Iced Tea" Iced Tea
@click="add" the click's event object [object PointerEvent] in Chrome
@click="add()" undefined empty

Without parentheses, Vue gives add the event object: an object with details about that click. That object goes into the array and shows as the text [object PointerEvent], not a dish name. With empty parentheses, name is undefined, which shows as nothing.

Modifiers: .prevent, .stop, .once, and keyboard keys

A modifier is an ending written with a dot after the event name, such as .stop in @click.stop. This code uses three modifiers: .stop, .enter and .prevent.

const log = ref([])

---template---
<div @click="log.push('Iced Tea card opened')">
  Iced Tea
  <button @click.stop="log.push('add Iced Tea')">Add</button>
</div>
<input @keyup.enter="log.push('search menu')" placeholder="Search menu" />
<form @submit.prevent="log.push('order sent')">
  <button>Send order</button>
</form>
<p>{{ log.join(" | ") }}</p>

Each modifier replaces one line of code that you would otherwise write yourself in the handler:

Modifier Same as Example
.prevent event.preventDefault() @submit.prevent: send a form without reloading the page
.stop event.stopPropagation() @click.stop: a click on the button doesn't reach the card around it
.once the handler runs only once @click.once: clicked three times, it still runs once
.enter, .esc only when that keyboard key is pressed @keyup.enter="search"

⚠️ Clicked, but the result is odd

TL;DR: @click="add" (short for v-on:click) runs the function add when the element is clicked. The value can be a function name, or a short statement like count++. Without parentheses, Vue gives the function the event object (details about what happened, such as the clicked element). With parentheses, you decide its arguments, and in the template $event holds that event object. Modifiers (endings with a dot after the event name) replace small bits of code. .prevent calls preventDefault() and stops the browser's default action, such as a form reloading the page. .stop keeps the event from spreading to its parent element (the element around it). .once runs only once, and .enter runs only when Enter is pressed. Don't use a plain HTML onclick, and in the script change a ref through .value.

⚡ @click runs code

Write:

const count = ref(0)

function add() {
  count.value++
}

---template---
<p>Iced Tea × {{ count }}</p>
<button @click="add">Add</button>
<button @click="count = 0">Cancel</button>

Meaning: This code counts Iced Tea orders. @click="add" is short for v-on:click="add": when the button is clicked, run the function add. The value can be a function name from the script, or one short statement such as count = 0. In the template, a ref is used without .value.

⚡ With or without parentheses

Write:

<button @click="add('Iced Tea')">Add Iced Tea</button>
<button @click="add">No parentheses</button>
Guess first: function add(name) puts name into the orders. When "No parentheses" is clicked, what goes in?

[object PointerEvent], not a dish name. Without parentheses, Vue calls the function itself and gives it the event object: an object with details about that click.

Meaning: With parentheses, you make the call, so you decide its arguments. If the buttons are made with v-for="m in menu", write @click="add(m.name)", so each button sends its own dish name. If you need the event object too, use $event, the special name for the event object in the template.

⚡ Modifiers: .prevent and .stop

Write:

<form @submit.prevent="log.push('order sent')">
  <button>Send order</button>
</form>

Meaning: A modifier is an ending with a dot after the event name. log here is an array of notes. .prevent is the same as event.preventDefault(): it stops the browser's default action, so sending the form doesn't reload the page. .stop is the same as event.stopPropagation(): a click on the button doesn't also count as a click on its parent element (the element around it).

What you will learn