Using Svelte's {#each}
functionality, I am reactively loading elements in the following way:
{#each $items as item}
<div>
<Button on:click={someFunction}>{item.text}</Button> (*)
</div>
{/each}
(*) a component that forwards its on:click
I am trying to find a way to reference the actual DOM element when calling someFunction by clicking on the created item. Passing the index of the array item to the function does not provide me with a reference to the unique DOM element. How can this be achieved?
Here are the approaches I have attempted so far:
→ results inon:click={() => someFunction(this)}
undefined
→ results inon:click={(el) => someFunction(el)}
undefined
→ usingon:click={(e) => someFunction(e)}
e.target
returns the clicked button, but accessing the div would require.parentElement
, which doesn't seem very Svelte-like.on:click={someFunction}
combined withbind:this={anItem}
→ only returns the last created element in the{#each}
block.