Binding in SVELTE

series on svelte part 6

ยท

3 min read

Binding in SVELTE

Hey folks ,welcome back to another interesting and important tutorial. Till now we have covered introduction , setup , condition based statements and loops , events and props .Today we are going to discuss Binding in Svelte.

What is Binding Binding means to bind / hold /stick something . Here also it has somewhat similar mean. We bind values of variable , input fields ,functions etc. to variables or events .

Why we need this Binding We have a input box and we want the value of input field on the browser. Generally we create an event handler that store the value of input.

General way is given below

<script>

    let name=""
    function handleInput()
    {
name=event.target.value;
    }
</script>

<input value={name} on:input={handleInput}>

<h1>Hello {name}!</h1>

In above example we create an event handler to store name. If there are many input fields ,we will write same event handlers many time to store each value but it will be a bit hectic and time consuming(repetition of same code )

Instead, we can use the bind:value directive

<script>
    let name=""
</script>

<input bind:value={name} >

<h1>Hello {name}!</h1>

It will work exactly same as handler work. It binds the value of input box to the variable 'name'. For lots of input fields we have to define same no. of variables only .

Similarly we can bind different things. We can bind a single state from more than 1 fields also.

<label>
    <input type=number bind:value={b} min=0 max=10>
    <input type=range   bind:value={b} min=0 max=10>
</label>

this will point to same state and handled by both input field simultaneously . Bind on checkboxes :

<script>
    let story = false;
</script>

<input type=checkbox bind:checked={story}>

This will bind to 'variable' whether its checked or not, it is better to have boolean value to binded variable.(for bind:checked)

If you have multiple inputs relating to the same value, you can use bind:group along with the value attribute like radio button or select . Add bind:group to each input: It can be implemented on check box .In this case the binded variable will be an array .

<script>
let fruits = ['grapes'];
</script>
<label>
    <input type=checkbox bind:group={fruits} name="fruits" value="Apple">
apple
</label>
<label>
    <input type=checkbox bind:group={fruits} name="fruits" value="Banana">
banana
</label>

In the above example , fruits is binded using group .It will contain the values , those are checked .If their are many options to check we can use an each loop .It will decrease the time and repetition of the code .


{#each fruits as fruit}
    <label>
        <input type=checkbox bind:group={fruits} name="fruits" value={fruit}>
        {fruit}
    </label>
{/each}

fruits is an array over which we are looping.

That's all for this tutorial for this tutorial .In the next we will continue next remaining Bind functionality.

Thanks for reading . I hope it has been a helpful read. Please leave any questions or comments below. Don't forget to like and share the article if it helps you in any way. Stay curious, cheers.