Author -  Sai gowtham

Svelte.js Event modifiers tutorial

In this tutorial, we are going to learn about event modifiers in svelte by using examples.

Event Modifiers

In svelte.js, we have event modifiers which helps us to modify the default behavior of dom events.

once modifier

The once modifier helps us to trigger the event only one time.

example:

Modifiers.svelte
<script>
   function handleClick(){
       alert('You can see me once')
   }

</script>

<button on:click|once={handlerClick}>Click me</button>

In the above code, we have added once modifer to on:click event so that we can only fire the click event once.

preventDefault

The preventDefault modifier helps us to solve the default loading behavior of the browser whenever we submit a form.

example:

Form.svelte
<script>
  function handleSubmit() {
    console.log("success");
  }
</script>

<form on:submit|preventDefault={handleSubmit}>  <input type="text" placeholder="Name" />
  <button>submit</button>
</form>

stopPropagation modifier

stopPropagation modifier helps us to stop reaching the event to the next element.

example:

Event.svelte
<style>
  div {
    padding: 1rem;
    border: 1px solid;
  }
</style>

<div on:click={() => console.log('Outer div')}>
  <h1>Outer div</h1>
  <div on:click|stopPropagation={() => console.log('inner div')}>
    <h1>Inner div</h1>
  </div>
</div>

In the above code, if we click on an Inner div it only fires the event on it instead of propagating it to Outer div

capture Modifier

It helps us to fire the event in capture phase instead of the bubbling phase.

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2023 (Beginner + Advanced)
JavaScript - The Complete Guide 2023 (Beginner + Advanced)
116,648 students enrolled
52 hours of video content
$14.99 FROM UDEMY
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
631,582 students enrolled
49 hours of video content
$24.99 FROM UDEMY
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
203,937 students enrolled
31.5 hours of video content
$14.99 FROM UDEMY