Basic Event Handlers
Click Events
The most common event isonclick:
Event Handler Syntax
Event handlers use themove |event| syntax:
Working with State
Updating Signals
Combine event handlers with signals for reactive UIs:Accessing Event Data
Mouse Events
Available Mouse Events
Mouse Event Data
Keyboard Events
Handling Keyboard Input
Keyboard Event Data
Form Events
Input Events
Typed Input Values
Useparsed() to get typed values:
Form Submission
Event Propagation
Stopping Propagation
Prevent events from bubbling up to parent elements:Preventing Default Behavior
Prevent the browser’s default action:Other Common Events
Focus Events
Scroll Events
Media Events
Event Handler Patterns
Sharing Event Handlers
Event Handlers with Parameters
Async Event Handlers
Event handlers can be async:Complete Event List
Mouse Events
onclick- Element clickedondoubleclick- Element double-clickedoncontextmenu- Right-click menuonmousedown- Mouse button pressedonmouseup- Mouse button releasedonmousemove- Mouse moved over elementonmouseenter- Mouse enters elementonmouseleave- Mouse leaves elementonmouseover- Mouse over element (bubbles)onmouseout- Mouse out of element (bubbles)
Keyboard Events
onkeydown- Key pressedonkeyup- Key releasedonkeypress- Key pressed (deprecated, use onkeydown)
Form Events
oninput- Input value changedonchange- Input change committedonsubmit- Form submittedonfocus- Element focusedonblur- Element lost focus
Other Events
onscroll- Element scrolledonwheel- Mouse wheel scrolledondrag- Element being draggedondrop- Element droppedonresize- Element resized
Best Practices
- Use
moveclosures: Event handlers need to own their captured data - Prevent default when needed: Use
event.prevent_default()for forms and links - Stop propagation carefully: Only stop propagation when necessary
- Keep handlers simple: Extract complex logic into separate functions
- Handle errors: Gracefully handle errors in async handlers
Next Steps
State Management
Learn more about managing state with signals
Conditional Rendering
Control what gets rendered