> Canonical URL: https://codingcat.dev/course/svelte/lesson/basic-events

# Learn Svelte: The Ultimate Guide - Basic Events | Learn Svelte: The Ultimate Guide | CodingCat.dev Course

[🧶 Learn Svelte: The Ultimate Guide](/course/svelte)Section 1.6 · Lesson 6 of 14[![](/logos/tags/svelte-symbol-light.svg)![](/logos/tags/svelte-symbol-dark.svg)Svelte](/topic/svelte)[![](/logos/tags/javascript-symbol-light.svg)![](/logos/tags/javascript-symbol-dark.svg)JavaScript](/topic/javascript)[![](/logos/tags/css-symbol-light.svg)![](/logos/tags/css-symbol-dark.svg)CSS](/topic/css)[← Prev Lesson](/course/svelte/lesson/inputs-bindings-and-reactivity)[Next Lesson →](/course/svelte/lesson/component-events-forwarding)

# Learn Svelte: The Ultimate Guide - Basic Events

![](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/f45bdcd180e2b7158e4deae3989a6e5aba0cec7e-1920x1080.png?w=1920&h=1080&q=82&fit=max&auto=format)[@CodingCatDev62.5K / 100K Subscribers Goal](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Subscribe on YouTube→](https://www.youtube.com/@codingcatdev?sub_confirmation=1)[Watch on YouTube App ↗](https://www.youtube.com/watch?v=pEf3V5fpCJc&list=UUnKZ8gEb78zXKMi1ns-IQ2g)Lesson 6 / 14Course Lessons Outline▾1Svelte
- [1.1Learn Svelte: The Ultimate Guide - What is Svelte?](/course/svelte/lesson/what-is-svelte)
- [1.2Learn Svelte: The Ultimate Guide - Getting Started](/course/svelte/lesson/getting-started)
- [1.3Learn Svelte: The Ultimate Guide - Starting to Code](/course/svelte/lesson/starting-to-code)
- [1.4Learn Svelte: The Ultimate Guide - Components and Props](/course/svelte/lesson/components-and-props)
- [1.5Learn Svelte: The Ultimate Guide - Inputs Bindings and Reactivity](/course/svelte/lesson/inputs-bindings-and-reactivity)
- [1.6Learn Svelte: The Ultimate Guide - Basic Events](/course/svelte/lesson/basic-events)
- [1.7Learn Svelte: The Ultimate Guide - Component Events and Forwarding](/course/svelte/lesson/component-events-forwarding)
- [1.8Learn Svelte: The Ultimate Guide - Conditionals and Loops](/course/svelte/lesson/conditionals-and-loops)
- [1.9Learn Svelte: The Ultimate Guide - CSS Basics](/course/svelte/lesson/css-basics)
- [1.10Learn Svelte: The Ultimate Guide - How to call an API](/course/svelte/lesson/how-to-call-an-api)
- [1.11Learn Svelte: The Ultimate Guide - Await Block](/course/svelte/lesson/await-block)
- [1.12Learn Svelte: The Ultimate Guide - Transitions and Animations](/course/svelte/lesson/transitions-and-animations)
- [1.13Learn Svelte: The Ultimate Guide - Component Lifecycle](/course/svelte/lesson/component-lifecycle)
- [1.14Learn Svelte: The Ultimate Guide - Data Stores](/course/svelte/lesson/data-stores)

## DOM Events in Svelte

Svelte is a modern JavaScript framework that is quickly gaining popularity among web developers. One of the things that makes Svelte so powerful is its event handling system. Svelte's event handling system is simple, efficient, and easy to learn.

In this blog post, we will discuss DOM events in Svelte. We will learn how to listen for DOM events, how to handle DOM events, and how to prevent default actions.

## What are DOM Events?

DOM events are events that are generated by the DOM. DOM events are triggered by user interaction, such as mouse clicks, key presses, and scroll events. DOM events can also be triggered by changes to the DOM, such as when an element is added or removed from the DOM.

## How to Listen for DOM Events

Svelte provides a number of ways to listen for DOM events. The most common way to listen for DOM events is to use the `on:eventname` directive. The `on:eventname` directive takes a function as its value. The function will be called whenever the event is triggered.

For example, the following code listens for the `click` event on a button:

Copied! 🐾
" aria-label="Copy code" title="Copy code to clipboard">

```
 {
		// do something when the button is clicked
	}}
>
	Click Me!

```

## How to Handle DOM Events

The function that is passed to the `on:eventname` directive can be used to handle the event. The function can be used to change the DOM, update the state of the component, or call a function.

For example, the following code changes the text of a paragraph when the button is clicked:

Copied! 🐾
" aria-label="Copy code" title="Copy code to clipboard">

```

This is a paragraph.

 {
		// change the text of the paragraph
		paragraph.textContent = 'This is a new paragraph.';
	}}
>
	Click Me!

```

## How to Prevent Default Actions

The `on:eventname` directive also takes a modifier called `preventDefault`. The `preventDefault` modifier can be used to prevent the default action of the event from being triggered.

For example, the following code prevents the default action of the `click` event from being triggered:

Copied! 🐾
" aria-label="Copy code" title="Copy code to clipboard">

```
 {
		// do something when the button is clicked
	}}
	preventDefault
>
	Click Me!

```

## Conclusion

DOM events are a powerful way to interact with the user and respond to changes in the DOM. Svelte's event handling system is simple, efficient, and easy to learn. By understanding how to use DOM events, we can build web applications that are both interactive and responsive.

✏️ Visual Architecture Diagram

## how this course connects together.

Follow AJ from first principles to production-ready architecture.

Let’s build it!✏️ Visual Diagram: Learn Svelte: The Ultimate Guide: Learn Svelte: …✓ Good PatternVISUAL DIAGRAM[Diagram Walkthrough]Step-by-step diagram breakdown for Learn Svelte: The Ultimate Guide: Learn Svelte: The Ultimate Guide - Basic Events: start with Svelte, implement Learn Svelte: The Ultimate Guide, and ship to production.AAA Contrast Verified

## related deep dives.

[View All&rarr;](/blog)[![The Odin Project 1: Web Development Foundations (HTML, CSS, Flexbox & JS Basics)](https://cdn.sanity.io/images/hfh83o0w/production/fce6fcae58667ff3143db133eb6b57fbd9ec471f-1376x768.png?w=1376&q=82&fit=max&auto=format)](/course/odin-web-development-foundations)November 4, 2026[![](/logos/tags/html-symbol-light.svg)![](/logos/tags/html-symbol-dark.svg)HTML](/topic/html)[![](/logos/tags/css-symbol-light.svg)![](/logos/tags/css-symbol-dark.svg)CSS](/topic/css)[![](/logos/tags/javascript-symbol-light.svg)![](/logos/tags/javascript-symbol-dark.svg)JavaScript](/topic/javascript)[+1](/course/odin-web-development-foundations)

### [The Odin Project 1: Web Development Foundations (HTML, CSS, Flexbox & JS Basics)](/course/odin-web-development-foundations)

Master the complete Odin Project Foundations path modernized for 2026: how the web works, semantic HTML5, CSS Box Model & Flexbox, and JavaScript DOM fundamentals.

[![Alex Patterson](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/a606dfaa381aab08ddbe16a33fe1d3e1fb37c927-400x400.jpg?w=96&h=96&q=82&fit=max&auto=format)Alex Patterson](/author/alex-patterson)[![The Odin Project 4: JavaScript Deep Dive (Closures, Event Loop, Async & TDD)](https://cdn.sanity.io/images/hfh83o0w/production/dd88ac44f50d7e5f3da1d21184bb96cb032c66fc-1376x768.png?w=1376&q=82&fit=max&auto=format)](/course/odin-javascript-deep-dive)November 4, 2026[![](/logos/tags/javascript-symbol-light.svg)![](/logos/tags/javascript-symbol-dark.svg)JavaScript](/topic/javascript)[![](/logos/tags/typescript-symbol-light.svg)![](/logos/tags/typescript-symbol-dark.svg)TypeScript](/topic/typescript)[#Testing](/topic/testing)[+1](/course/odin-javascript-deep-dive)

### [The Odin Project 4: JavaScript Deep Dive (Closures, Event Loop, Async & TDD)](/course/odin-javascript-deep-dive)

Master lexical closures, ES Modules, the V8 Event Loop (microtasks vs. macrotasks), Promise concurrency, and Test-Driven Development with Vitest.

[![Alex Patterson](https://cdn.sanity.io/media-libraries/mlttp0BZbCpV/images/a606dfaa381aab08ddbe16a33fe1d3e1fb37c927-400x400.jpg?w=96&h=96&q=82&fit=max&auto=format)Alex Patterson](/author/alex-patterson)[Course Outline](/course/svelte)

## Learn Svelte: The Ultimate Guide

6/141

### Svelte

- [1.1Learn Svelte: The Ultimate Guide - What is Svelte?](/course/svelte/lesson/what-is-svelte)
- [1.2Learn Svelte: The Ultimate Guide - Getting Started](/course/svelte/lesson/getting-started)
- [1.3Learn Svelte: The Ultimate Guide - Starting to Code](/course/svelte/lesson/starting-to-code)
- [1.4Learn Svelte: The Ultimate Guide - Components and Props](/course/svelte/lesson/components-and-props)
- [1.5Learn Svelte: The Ultimate Guide - Inputs Bindings and Reactivity](/course/svelte/lesson/inputs-bindings-and-reactivity)
- [1.6Learn Svelte: The Ultimate Guide - Basic Events▶](/course/svelte/lesson/basic-events)
- [1.7Learn Svelte: The Ultimate Guide - Component Events and Forwarding](/course/svelte/lesson/component-events-forwarding)
- [1.8Learn Svelte: The Ultimate Guide - Conditionals and Loops](/course/svelte/lesson/conditionals-and-loops)
- [1.9Learn Svelte: The Ultimate Guide - CSS Basics](/course/svelte/lesson/css-basics)
- [1.10Learn Svelte: The Ultimate Guide - How to call an API](/course/svelte/lesson/how-to-call-an-api)
- [1.11Learn Svelte: The Ultimate Guide - Await Block](/course/svelte/lesson/await-block)
- [1.12Learn Svelte: The Ultimate Guide - Transitions and Animations](/course/svelte/lesson/transitions-and-animations)
- [1.13Learn Svelte: The Ultimate Guide - Component Lifecycle](/course/svelte/lesson/component-lifecycle)
- [1.14Learn Svelte: The Ultimate Guide - Data Stores](/course/svelte/lesson/data-stores)

@CodingCatDev on YouTube62.5K / 100K

Subscribe for new course lessons and architecture deep dives every week.

[Subscribe for New Lessons →](https://www.youtube.com/@codingcatdev?sub_confirmation=1)