Svelte.js Event modifiers tutorial

Svelte.js Event modifiers tutorial

In this post we will give you information about Svelte.js Event modifiers tutorial. Hear we will give you detail about Svelte.js Event modifiers tutorialAnd how to use it also give you demo for it if it is necessary.

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.

Hope this code and post will helped you for implement Svelte.js Event modifiers tutorial. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US