concept input in category svelte

This is an excerpt from Manning's book Svelte and Sapper in Action.
The
inputHTML element is an example of an empty element, which means it cannot have child nodes. When empty elements are used in Svelte components, they do not need to be terminated with/>. However, if Prettier is used to format the code, it will change empty elements so they’re terminated this way. This is why some code examples in this book show terminated empty elements.Now we can enter a name, but it doesn’t change the greeting. We need to add event handling so the value of the
namevariable is updated when the user types in the input. We can do this using an inline arrow function or a reference to a function defined in thescriptelement. Change theinputto the following:
<script> import {afterUpdate, beforeUpdate} from 'svelte'; let input, name, selectionEnd, selectionStart; beforeUpdate(() => { if (input) ({selectionStart, selectionEnd} = input); #1 }) afterUpdate(() => { input.setSelectionRange(selectionStart, selectionEnd); input.focus(); }); </script> <input bind:this={input} bind:value={name}> #2 <button on:click={() => name = name.toUpperCase()}>UPPER</button>