Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Directives  ›  Capstone Project

Event and Key Modifiers

Event Modifiers 13 minModifiers are dot suffixes on v-on that handle common event chores like preventing defaults or filtering keys, right in the template.

Event and Key Modifiers

Shelf's search is a small form. Press Enter and the browser reloads the page, wiping your results. You could write event.preventDefault() at the top of every handler and check event.key === 'Escape' in others. Vue offers a tidier way: little suffixes on v-on that do the chores before your code runs.

The rule

A dot suffix does the chore; your handler keeps only the logic.

Event modifiers handle common event plumbing:

Modifier Effect
.prevent calls event.preventDefault()
.stop calls event.stopPropagation(), so parents do not hear it
.self runs only if the event started on this element, not a child
.once runs at most once
.capture listens in the capture phase
.passive tells the browser the handler will not prevent the default, good for scroll

Key modifiers filter keyboard events by key: .enter, .esc, .space, .tab, .delete, .up, .down, .left, .right. System modifiers require a key to be held: .ctrl, .alt, .shift, .meta, and .exact means "these and no others". Mouse buttons have .left, .right and .middle.

<form @submit.prevent="search">
  <input v-model="query"
         @keyup.esc="query = ''"
         @keydown.ctrl.enter="searchEverywhere" />
</form>

<div class="modal-backdrop" @click.self="closeModal">
  <div class="modal">...</div>
</div>

<a href="/books/1" @click.stop.prevent="openPreview(1)">Dune</a>
  • Enter in the input submits the form; .prevent stops the reload, and search runs.
  • Escape clears the box, without any if in the handler.
  • Ctrl + Enter runs a wider search.
  • Clicking the dark backdrop closes the modal, but clicks inside the modal do not, because their target is a child.

Modifiers chain, and order matters: they are applied left to right. @click.prevent.self prevents the default for clicks on the element and its children, while @click.self.prevent prevents it only for clicks on the element itself. Also, do not combine .passive with .prevent; passive promises you will not prevent the default, so the browser ignores the attempt.

The shortcut

When a handler starts with plumbing, turn the first line into a modifier. event.preventDefault() → .prevent. if (event.key !== 'Escape') return → .esc. if (event.target !== event.currentTarget) return → .self.

Cleaning up the search formStep 1 of 4
  1. Before

    function search(e) { e.preventDefault(); runSearch(query.value) } on @submit.

Drills

Answer fast — trust the rule.

quick drill

Which modifier stops a form submission from reloading the page?

quick drill

What does @keyup.enter="save" do?

quick drill

A backdrop has @click.self="close". The user clicks a button inside the modal on top of it. Does close run?

quick drill

A button inside a clickable card should not trigger the card's handler. Which modifier goes on the button's click?

quick drill

Which pair should never be combined?

Remember

  • Event modifiers (.prevent, .stop, .self, .once) and key modifiers (.enter, .esc, .ctrl) handle plumbing in the template.
  • Shortcut: plumbing on a handler's first line becomes a modifier.
  • Trap: modifier order changes meaning; .passive with .prevent does not work.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA tidy search box

Shelf's search is a form with one input. Pressing Enter should run the search without reloading the page, and pressing Escape should clear the box. Write the template and explain each modifier.