Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

How to bind events dynamically in Svelte?

$
0
0

In JSX it's possible to bind multiple events to a DOM element like this:

<input {...inputEvents}>

In Svelte, this is done manually.

<input on:input={inputHandler}>

This becomes tedious when you need to add multiple handlers (input, blur, focus) to multiple inputs of a form.

The only way I've found to solve this in Svelte is by using refs.

For example:

<input bind:this={myInput}>

And then somewhere either do this:

myInput.oninput = (event) => {  // do something}

Or:

myInput.addEventListener('click', (event) => {  // do something})

Is there a better way to bind events dynamically in Svelte?


Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>