Svelte Event Modifiers

Event modifiers adjust how a handler behaves without extra lines inside the function. Add a pipe character after the event name and list one or more modifier words. Svelte applies the behavior for you.

Using Modifiers

The most common need is stopping a form from reloading the page. The preventDefault modifier handles it in one word.

<script>
  let email = "";
  let status = "";

  function subscribe() {
    status = `Saved ${email}`;
  }
</script>

<form on:submit|preventDefault={subscribe}>
  <input type="email" bind:value={email} />
  <button>Join</button>
</form>
<p>{status}</p>

Without the modifier, the browser submits the form and reloads the page, wiping your variables.

Available Modifiers

Modifier             Effect
-------------------  --------------------------------------------
preventDefault       Blocks the browser default action
stopPropagation      Stops the event from reaching parent elements
once                 Runs the handler a single time
self                 Runs only when the element itself is clicked
capture              Listens during the capture phase
passive              Improves scroll performance for touch events
trusted              Runs only for real user actions

Event Travel Diagram

Events travel through the page in two directions. They move down from the window to the target and bubble back up through every parent.

window
  |  capture phase (down)
  v
<div class="card">
  |
  v
<button>   <-- target: the click happens here
  |
  |  bubble phase (up)
  v
<div class="card"> handler runs
  |
  v
window handler runs

Stopping Bubbles

A click on an inner button reaches the card around it. The stopPropagation modifier ends the journey at the button.

<div on:click={() => (message = "Card clicked")}>
  <button on:click|stopPropagation={() => (message = "Button only")}>
    Save
  </button>
</div>

Running Once

The once modifier removes the handler after its first run. A welcome banner or a one-time tip suits this pattern.

<button on:click|once={showTip}>Show Tip</button>

Clicking The Element Itself

A modal window often sits inside a dark backdrop. Clicks on the white dialog should not close it, and clicks on the dark area should. The self modifier makes that distinction.

<div class="backdrop" on:click|self={closeDialog}>
  <div class="dialog">
    Dialog content here
  </div>
</div>

Chaining Modifiers

Modifiers stack in any order. Each pipe adds another behavior.

<form on:submit|preventDefault|once={sendOnce}>
  <button>Send</button>
</form>

A Layman Analogy

Think of a message passed along a line of office workers. Bubbling means each worker reads the message and hands it to the next one. The stopPropagation modifier works like a worker who keeps the message and ends the chain. The once modifier works like a note that destroys itself after reading.

Runes Mode Note

Components in Svelte 5 runes mode use onclick and similar attributes, and those do not support modifiers. Call event.preventDefault() or event.stopPropagation() inside the handler function.

<script>
  function handleSubmit(event) {
    event.preventDefault();
    // save the form data here
  }
</script>

<form onsubmit={handleSubmit}>...</form>

Common Mistakes To Avoid

  • Forgetting preventDefault on forms and links that run custom code
  • Using stopPropagation everywhere, which breaks features that listen on parent elements
  • Writing modifier names in the wrong case, since they are case-sensitive

Key Points

  • Modifiers attach to an event name with a pipe character
  • preventDefault, stopPropagation, once, and self solve most everyday needs
  • Several modifiers can chain on the same event
  • Runes mode handles the same tasks inside the handler function