Svelte · Junior · Question 24 of 76

How do you get a reference to a DOM element?

<script>
  let canvas;
</script>

<canvas bind:this={canvas}></canvas>

Explanation

The value is undefined until mount, so use it in $effect, onMount or event handlers.

Further reading

Problem with a question

Svelte · Junior · question 24

Test yourself

Take the Svelte · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz