Initiating actions¶
HTML elements can trigger any number of application actions in response to any number of events.
More than one element can initiate the same action.
An action signals that something has happened, but does nothing by itself.
All event types supported by the document
object can be used, along with
the following custom ones:
reveal
— triggered when an element becomes visible in the viewport, either on initial page load or after scrollingconceal
— triggered when an element leaves the viewport after scrollingnavigate
— triggered by the History APIresult
— triggered after a successful request and rendering of a response (you can rely on new attributes being present and working when this event is triggered)
All events bubble up to the root of the DOM tree.
Attributes¶
- Any attribute that starts with
on:
and contains a space-separated list of actions to initiate - Any attribute that starts with
event:
and contains a comma-separated list of name-value pairs separated by=
The value part is optional, and the key will be checked only for truthiness if omitted.
Each key and value pair must match the event object; otherwise, the event handling is skipped.
Can be used, for example, to associate hotkeys with an element.
Basic Example¶
- Moving the mouse cursor into the button initiates the
doSomething
action - Clicking the button initiates two actions:
loadData
andupdateCounter
- Nothing happens as a result, since none of the elements subscribe to any of these actions
1 2 3 4 5 6 |
|
Hotkey Example¶
- putting the cursor inside of the input and pressing
Alt+Esc
initiates thehotkeysAreCool
action
1 2 3 4 |
|
Bubbling Example¶
- When you click the image, the button’s
handleClick
action is initiated because it is the first matchingon:<event name>
on a parent element. - This works with any number of nesting levels (e.g., global hotkeys on the body element)
1 2 3 |
|
Warning
Please be mindful that all actions are, by design, global within the page they are used on.
It is your responsibility to generate unique action names to ensure that no other parts of the application are affected by the actions you trigger.
Example of a potential pitfall:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
In the example above, we can imagine that it is the same button component, simply rendered twice.
But, they are two distinct elements on the page that trigger and subscribe to
the same action loadData
.
Thus, when you click either of them, two network requests will be sent to the server.