Documentation menu

Events and Commands

Attach an event handler from h to an element. Pass it commands from github.com/franchb/htmgo/framework/v2/js to update the page in the browser.

This form disables its submit button while the request is in flight and restores it when the request finishes.

Copy
 1func MyForm() *h.Element {
 2	return h.Form(
 3		h.PostPartial(Submit),
 4		h.NoSwap(),
 5		h.Button(
 6			h.Type("submit"),
 7			h.Text("Submit"),
 8			h.HxBeforeRequest(
 9				js.SetDisabled(true),
10				js.SetText("Submitting…"),
11			),
12			h.OnEvent("htmx:finally:request",
13				js.SetDisabled(false),
14				js.SetText("Submit"),
15			),
16		),
17	)
18}
19
20func Submit(ctx *h.RequestContext) *h.Partial {
21	return h.EmptyPartial()
22}

The bundled Trigger Children extension forwards the form’s request events to the button.

Choose an event

Common helpers include h.OnClick, h.OnSubmit, h.HxBeforeRequest, h.HxAfterRequest, and h.HxOnAfterSwap.

Use h.OnEvent with an event constant from hx or a DOM handler name such as onblur. htmx event names use the htmx: prefix and colon separators.

Copy
1h.Input("text",
2	h.OnEvent(hx.BlurEvent, js.SetValue("Input was blurred")),
3)

Run JavaScript

Use js.EvalJs to run code inside a handler. The function receives the current element as self and the event as event (also available as e).

Copy
1func MyButton() *h.Element {
2	return h.Button(
3		h.Type("button"),
4		h.Text("Click me"),
5		h.OnClick(
6			js.EvalJs("self.textContent = 'Clicked';"),
7		),
8	)
9}
Browse the js command referenceView the button demo