Sandbox
Feel free to play around with the example below to see how the web page reacts to your code changes in a consequence-free environment. You won't break or overwrite anything so go for gold.
This project introduces Svelte-specific if statements inside your HTML.
You can still write normal if statements inside the <script></script>
tags, but you won't have to as often.
As with the previous projects, you'll use some Svelte-specific code in curly braces { }
.
Specifically, we'll use the {#if}
, {:else if}
, {:else}
, and {/if}
to decide what HTML to have on the page.
Our demo is going to be a simple app that tells you if the variable is less, more, or equal to 2.
Let's compare the jQuery version of Is It More Than 2? with the Svelte version.
<label>
Number:
<input id="numInput" type="number">
</label>
<button id="btnCheck">Check</button>
<p id="txtResult"></p>
$('#btnCheck').click( ()=> {
let input = $('#numInput').val()
let number = Number(input)
let result = ''
if (number < 2) {
result = number + ' is less than 2.'
} else if (number > 2) {
result = number + ' is more than 2.'
} else {
result = number + ' is 2.'
}
$('#txtResult').text(result)
})
<script>
let number = 0
</script>
<label>
Number:
<input type="number" bind:value={number}>
</label>
{#if number < 2}
<p>{number} is less than 2.</p>
{:else if number > 2}
<p>{number} is more than 2.</p>
{:else}
<p>{number} is 2.</p>
{/if}
npm install
and then npm run dev
.https://localhost:3000
.<input type="range" min="0" max="100" bind:value={temp}>
Feel free to play around with the example below to see how the web app reacts to your changes in a consequence-free environment.
Feel free to play around with the example below to see how the web page reacts to your code changes in a consequence-free environment. You won't break or overwrite anything so go for gold.