Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 39 ยท JSX and Control Flow

Events, Styling and classList

Handle DOM events in SolidJS with onClick and on: prefixes, event delegation, bound handlers, plus dynamic style, class and classList patterns.

Handling user input and styling elements are everyday tasks. Solid keeps both close to the plain DOM: events are native browser events, and styling uses ordinary class and style with a couple of reactive helpers. This lesson shows the event syntax, how delegation works, and the best ways to switch classes and styles.

Event handlers

Attach a handler with an on prefix and the event name in camel case:

JSX
<button onClick={() => setCount(count() + 1)}>Add</button>
<input onInput={(e) => setText(e.currentTarget.value)} />
<form onSubmit={(e) => { e.preventDefault(); save(); }}>...</form>
  • The handler is called with the native event, not a synthetic wrapper.
  • Use e.currentTarget for the element the handler is attached to; with delegated events it is correctly set by Solid.
  • Handlers are not reactive: onClick={handler()} calls the function immediately. Pass the function itself, or an inline arrow.

Solid creates the handler once. When you pass a function that closes over signals, it reads the latest value when the event fires, because you call the getters inside it.

Click, input and key events

Delegated and native events

For common bubbling events (click, input, keydown, pointerdown, and others), Solid attaches one listener on the document and routes events to the right element. That keeps memory low for big lists. Two practical consequences:

  • If you call e.stopPropagation() on a native listener registered with addEventListener, delegated handlers further up may not fire. Mixing styles can surprise you.
  • Events that do not bubble (such as focus, blur, scroll, mouseenter) are attached directly to the element.

When you need a true native listener, for example for custom events or to set options like passive or capture, use the on: namespace:

JSX
<div on:scroll={(e) => setTop(e.currentTarget.scrollTop)} />
<my-widget on:custom-event={handler} />

on: attaches with addEventListener directly and is not delegated. Its counterpart oncapture: registers in the capture phase.

Handlers with data

You can pass data to a handler as a tuple: a function and a value. Solid calls the function with that value as the first argument and the event second.

JSX
<For each={items()}>
  {(item) => <button onClick={[remove, item.id]}>Delete</button>}
</For>

This avoids creating a new closure per row and is a small, optional optimization. An inline arrow () => remove(item.id) is equally correct.

Dynamic styles

Use an object (property names with dashes in quotes, or camel case), or a string.

JSX
<div style={{ width: `${pct()}%`, "background-color": color() }} />

For individual properties, the style: namespace is neat and avoids rebuilding the whole object:

JSX
<div style:opacity={visible() ? 1 : 0} style:--accent={theme().accent} />

Custom properties (--accent) work, which makes it easy to pass values to your stylesheet.

Classes and classList

class takes a string (any expression). For toggling several classes by condition, use classList, an object whose keys are class names and whose values are booleans:

JSX
<li
  class="todo"
  classList={{ done: todo.done, selected: selectedId() === todo.id }}
>

Rules of thumb:

  • class provides the fixed or computed base; classList toggles extras.
  • Do not set the same class from both class and classList in one element, as they can overwrite each other.
  • For class names with dashes, quote the key: { "is-active": on() }.
classList and style: namespace

Forms and controlled inputs

Setting value={text()} keeps the DOM property in step with a signal, and onInput updates the signal. There is no onChange quirk like in React: onInput fires per keystroke, native change fires on commit. Form-specific patterns are in Forms and Controlled Inputs.

Common mistakes

  • Calling the handler instead of passing it: onClick={save()} runs save during render.
  • Using e.target when you meant e.currentTarget. target can be a child of the element.
  • Mixing onClick and addEventListener("click") with stopPropagation. Delegation can make ordering confusing.
  • Putting class and classList on the same class name. Keep them disjoint.
  • Expecting onChange to fire per keystroke. Use onInput.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does a Solid onClick handler receive?

    Solid uses native events; delegation is an internal detail.
  2. 2. Which syntax attaches a non-delegated native listener?

    The on: namespace calls addEventListener directly on the element.
  3. 3. What is classList used for?

    Keys are class names and values decide whether each class is present.
  4. 4. Which event should you use for per-keystroke input updates?

    input fires on every edit; change fires on commit or blur.

Summary

  • Events use onClick-style props and receive native events; common ones are delegated to the document.
  • Use on: for non-delegated listeners and custom events.
  • Style with an object or style: properties; toggle classes with classList.
  • Pass handlers, never call them in markup.

Next: Refs and Directives