v0.20.230 - 6.1 / 6.2

setAttribute

Imperative helper for setting an attribute on an element. Unwraps reactive accessors, and removes the attribute when the value is false, null or undefined.

# Signature

# Arguments

nametypedescription
nodeElementtarget element
namestringattribute name (case sensitive)
valuestring | number | boolean | signalattribute value. A function is treated as a reactive accessor and tracked; true sets the attribute to the empty string; false / null / undefined remove the attribute.

Returns: void

# When to use

In JSX, prefer writing attributes directly — <div id={id}/> already flows through setAttribute. Reach for the function when you are working outside JSX: building elements via document.createElement, driving a ref from an effect, writing a propsPlugin, or bridging a third-party library.

# Snippet