Documentation menu
Getting Started
Core Concepts
Control
Interactivity
Pushing Data
HTMX Extensions
Miscellaneous
Configuration
Eval Commands
Pass an element and commands to js.EvalCommands to run the commands against it when an event fires.
Reference an element directly
func ToggleTextButton() *h.Element {
text := h.P(h.Text("Text Before"))
return h.Div(
h.Button(
h.Type("button"),
h.Text("Toggle Text"),
h.OnClick(
js.EvalCommands(
text,
js.ToggleText("Text Before", "Text After"),
),
),
),
text,
)
}
Copy
1func ToggleTextButton() *h.Element {
2 text := h.P(h.Text("Text Before"))
3 return h.Div(
4 h.Button(
5 h.Type("button"),
6 h.Text("Toggle Text"),
7 h.OnClick(
8 js.EvalCommands(
9 text,
10 js.ToggleText("Text Before", "Text After"),
11 ),
12 ),
13 ),
14 text,
15 )
16}The button toggles the paragraph text. js.EvalCommands assigns a selector to the referenced paragraph as it builds the elements.
Use a selector
Use js.EvalCommandsOnSelector to target an element using its CSS selector.
func ToggleTextButton() *h.Element {
text := h.P(h.Id("my-element"), h.Text("Text Before"))
return h.Div(
h.Button(
h.Type("button"),
h.Text("Toggle Text"),
h.OnClick(
js.EvalCommandsOnSelector(
"#my-element",
js.ToggleText("Text Before", "Text After"),
),
),
),
text,
)
}
Copy
1func ToggleTextButton() *h.Element {
2 text := h.P(h.Id("my-element"), h.Text("Text Before"))
3 return h.Div(
4 h.Button(
5 h.Type("button"),
6 h.Text("Toggle Text"),
7 h.OnClick(
8 js.EvalCommandsOnSelector(
9 "#my-element",
10 js.ToggleText("Text Before", "Text After"),
11 ),
12 ),
13 ),
14 text,
15 )
16}