x-on
====
`x-on` allows you to easily run code on dispatched DOM events.
Here's an example of simple button that shows an alert when clicked.
> `x-on` can only listen for events with lower case names, as HTML attributes are case-insensitive. Writing `x-on:CLICK` will listen for an event named `click`. If you need to listen for a custom event with a camelCase name, you can use the [`.camel` helper](#camel) to work around this limitation. Alternatively, you can use [`x-bind`](/directives/bind#bind-directives) to attach an `x-on` directive to an element in javascript code (where case will be preserved).
[Shorthand syntax](#shorthand-syntax)
-------------------------------------
If `x-on:` is too verbose for your tastes, you can use the shorthand syntax: `@`.
Here's the same component as above, but using the shorthand syntax instead:
> Despite not being included in the above snippet, `x-on` cannot be used if no parent element has `x-data` defined. [→ Read more about `x-data`](/directives/data)
[The event object](#the-event-object)
-------------------------------------
If you wish to access the native JavaScript event object from your expression, you can use Alpine's magic `$event` property.
In addition, Alpine also passes the event object to any methods referenced without trailing parenthesis. For example:
[Keyboard events](#keyboard-events)
-----------------------------------
Alpine makes it easy to listen for `keydown` and `keyup` events on specific keys.
Here's an example of listening for the `Enter` key inside an input element.
You can also chain these key modifiers to achieve more complex listeners.
Here's a listener that runs when the `Shift` key is held and `Enter` is pressed, but not when `Enter` is pressed alone.
You can directly use any valid key names exposed via [`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values) as modifiers by converting them to kebab-case.
For easy reference, here is a list of common keys you may want to listen for.
Modifier
Keyboard Key
`.shift`
Shift
`.enter`
Enter
`.space`
Space
`.ctrl`
Ctrl
`.cmd`
Cmd
`.meta`
Cmd on Mac, Windows key on Windows
`.alt`
Alt
`.up` `.down` `.left` `.right`
Up/Down/Left/Right arrows
`.escape`
Escape
`.tab`
Tab
`.caps-lock`
Caps Lock
`.equal`
Equal, `=`
`.period`
Period, `.`
`.comma`
Comma, `,`
`.slash`
Forward Slash, `/`
[Mouse events](#mouse-events)
-----------------------------
Like the above Keyboard Events, Alpine allows the use of some key modifiers for handling `click` events.
Modifier
Event Key
`.shift`
shiftKey
`.ctrl`
ctrlKey
`.cmd`
metaKey
`.meta`
metaKey
`.alt`
altKey
These work on `click`, `auxclick`, `context` and `dblclick` events, and even `mouseover`, `mousemove`, `mouseenter`, `mouseleave`, `mouseout`, `mouseup` and `mousedown`.
Here's an example of a button that changes behaviour when the `Shift` key is held down.
> Note: Normal click events with some modifiers (like `ctrl`) will automatically become `contextmenu` events in most browsers. Similarly, `right-click` events will trigger a `contextmenu` event, but will also trigger an `auxclick` event if the `contextmenu` event is prevented.
[Custom events](#custom-events)
-------------------------------
Alpine event listeners are a wrapper for native DOM event listeners. Therefore, they can listen for ANY DOM event, including custom events.
Here's an example of a component that dispatches a custom DOM event and listens for it as well.
When the button is clicked, the `@foo` listener will be called.
Because the `.dispatchEvent` API is verbose, Alpine offers a `$dispatch` helper to simplify things.
Here's the same component re-written with the `$dispatch` magic property.
[→ Read more about `$dispatch`](/magics/dispatch)
[Modifiers](#modifiers)
-----------------------
Alpine offers a number of directive modifiers to customize the behavior of your event listeners.
### [.prevent](#prevent)
`.prevent` is the equivalent of calling `.preventDefault()` inside a listener on the browser event object.
In the above example, with the `.prevent`, clicking the button will NOT submit the form to the `/foo` endpoint. Instead, Alpine's listener will handle it and "prevent" the event from being handled any further.
### [.stop](#stop)
Similar to `.prevent`, `.stop` is the equivalent of calling `.stopPropagation()` inside a listener on the browser event object.
In the above example, clicking the button WON'T log the message. This is because we are stopping the propagation of the event immediately and not allowing it to "bubble" up to the `
` with the `@click` listener on it.
### [.outside](#outside)
`.outside` is a convenience helper for listening for a click outside of the element it is attached to. Here's a simple dropdown component example to demonstrate:
Contents...
Contents...
In the above example, after showing the dropdown contents by clicking the "Toggle" button, you can close the dropdown by clicking anywhere on the page outside the content.
This is because `.outside` is listening for clicks that DON'T originate from the element it's registered on.
> It's worth noting that the `.outside` expression will only be evaluated when the element it's registered on is visible on the page. Otherwise, there would be nasty race conditions where clicking the "Toggle" button would also fire the `@click.outside` handler when it is not visible.
### [.window](#window)
When the `.window` modifier is present, Alpine will register the event listener on the root `window` object on the page instead of the element itself.
...
...
The above snippet will listen for the "escape" key to be pressed ANYWHERE on the page.
Adding `.window` to listeners is extremely useful for these sorts of cases where a small part of your markup is concerned with events that take place on the entire page.
### [.document](#document)
`.document` works similarly to `.window` only it registers listeners on the `document` global, instead of the `window` global.
### [.once](#once)
By adding `.once` to a listener, you are ensuring that the handler is only called ONCE.
### [.debounce](#debounce)
Sometimes it is useful to "debounce" an event handler so that it only is called after a certain period of inactivity (250 milliseconds by default).
For example if you have a search field that fires network requests as the user types into it, adding a debounce will prevent the network requests from firing on every single keystroke.
Now, instead of calling `fetchResults` after every keystroke, `fetchResults` will only be called after 250 milliseconds of no keystrokes.
If you wish to lengthen or shorten the debounce time, you can do so by trailing a duration after the `.debounce` modifier like so:
Now, `fetchResults` will only be called after 500 milliseconds of inactivity.
### [.throttle](#throttle)
`.throttle` is similar to `.debounce` except it will release a handler call every 250 milliseconds instead of deferring it indefinitely.
This is useful for cases where there may be repeated and prolonged event firing and using `.debounce` won't work because you want to still handle the event every so often.
For example:
...
...
The above example is a great use case of throttling. Without `.throttle`, the `handleScroll` method would be fired hundreds of times as the user scrolls down a page. This can really slow down a site. By adding `.throttle`, we are ensuring that `handleScroll` only gets called every 250 milliseconds.
> Fun Fact: This exact strategy is used on this very documentation site to update the currently highlighted section in the right sidebar.
Just like with `.debounce`, you can add a custom duration to your throttled event:
...
...
Now, `handleScroll` will only be called every 750 milliseconds.
### [.self](#self)
By adding `.self` to an event listener, you are ensuring that the event originated on the element it is declared on, and not from a child element.
In the above example, we have an `` tag inside the `