Documentation menu

Eval Commands

Pass an element and commands to js.EvalCommands to run the commands against it when an event fires.

Reference an element directly

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.

View the related demo

Use a selector

Use js.EvalCommandsOnSelector to target an element using its CSS selector.

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}