In most applications, you'll need to respond to the user's actions. In Svelte, this is done with the `on:[event]` directive.
### Element events
When used on an element, `on:click={handler}` is equivalent to calling `element.addEventListener('click', handler)`. When the element is removed, Svelte calls `removeEventListener` automatically.
```html
<!-- { title: 'Inline event handlers' } -->
<p>Count: {count}</p>
<buttonon:click="{() => count += 1}">+1</button>
```
```json
/* { hidden: true } */
{
count: 0
}
```
For more complicated behaviours, you'll probably want to declare an event handler in your `<script>` block:
- [`passive`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters) — improves scrolling performance on touch/wheel events (Svelte will add it automatically where it's safe to do so)
- [`once`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters) — removes the listener after the first invocation
Events are an excellent way for [nested components](docs#nested-components) to communicate with their parents. Let's revisit our earlier example, but turn it into a `<CategoryChooser>` component:
When the user clicks a button, the component will fire a `select` event, where the `event` object has a `category` property. Any component that nests `<CategoryChooser>` can listen for events like so:
Just as `this` in an element's event handler refers to the element itself, in a component event handler `this` refers to the component firing the event.
There is also a shorthand for listening for and re-firing an event unchanged.
```html
<!-- { repl: false } -->
<!-- these are equivalent -->
<Widgeton:foo="fire('foo', event)"/>
<Widgeton:foo/>
```
Since component events do not propagate as DOM events do, this can be used to pass events through intermediate components. This shorthand technique also applies to element events (`on:click` is equivalent to `on:click="fire('click', event)"`).