DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Telemetry Tax: Architecting Zero-Allocation Event Observability at 15B+ Daily Event Scale
  • Beyond Screenshots: Building Replayable Production Diagnostics for Hard-to-Reproduce Bugs
  • When Mobile Connections Break: Recovering Long-Running iOS Workflows With LangGraph and Event-Driven Backends
  • Event-Driven AI Systems With Kafka and Autonomous Agents

Trending

  • Policy-as-Code for AI Systems: Enforcing Governance at the Infrastructure Layer
  • Architecting for <1s Latency: Managing Eventual Consistency in Distributed Search Platforms
  • Why Your Unified API Strategy Will Break
  • Building Agentic RAG, Step by Step: From Static Retrieval to Reasoning Pipelines

How to Pass Arguments to Events Like On:Click in Svelte

Let's look at how to pass arguments to events in Svelte

By 
Johnny Simpson user avatar
Johnny Simpson
·
May. 30, 22 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

Svelte events are the way we add interactivity to components in Svelte. A common issue with Svelte events is adding arguments to functions called within them. For example, suppose we have a basic counter, which increases any time the user clicks on it:

HTML
 
<script>
    // we write export let to say that this is a property
    // that means we can change it later!
    let x = 0;
    const addToCounter = function() {
        ++x;
    }
</script>

<button id="counter" on:click={addToCounter}>{x}</button>

This works fine, but let's say we want to change it so that we increase the counter by a certain amount whenever it is clicked. We might try changing the code to something like this:

HTML
 
<script>
    // we write export let to say that this is a property
    // that means we can change it later!
    let x = 0;
    const addToCounter = function(amount) {
        x += amount;
    }
</script>

<button id="counter" on:click={addToCounter(5)}>{x}</button>

But this won't work - instead, we need to change our event to contain a function instead. To add arguments to our addToCounter function, we have to do something like this instead:

HTML
 
<button id="counter" on:click={() => addToCounter(5)}>{x}</button>

Here, we call a function, which returns the value produced by addToCounter. This also works for events, so if you want to pass the event or e object to your function, you could do something like this:

HTML
 
<button id="counter" on:click={(e) => addToCounter(e)}>{x}</button>
Svelte Event Pass (software)

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • The Telemetry Tax: Architecting Zero-Allocation Event Observability at 15B+ Daily Event Scale
  • Beyond Screenshots: Building Replayable Production Diagnostics for Hard-to-Reproduce Bugs
  • When Mobile Connections Break: Recovering Long-Running iOS Workflows With LangGraph and Event-Driven Backends
  • Event-Driven AI Systems With Kafka and Autonomous Agents

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook