Tags and Attributes

In htmgo, html is built using a set of functions that return *h.Element.

These functions are all defined in the 'h' package in htmgo/framework

htmgo provides methods to render most if not all html tags and attributes.

Example:

Copy
1h.Div(
2	h.Class("flex gap-2"),
3	h.Button(
4		h.Text("Submit"),
5	),
6)

All methods can be found in the 'h' package in htmgo/framework

h.Tag and h.Attribute are available to use when you need to render a tag or attribute that htmgo does not provide a method for.

Copy
1h.Tag("my-custom-tag",
2	h.Class("flex gap-2"),
3	h.Button(
4        h.Attribute("x-custom-attr", "my-value"),
5		h.Text("Submit"),
6	),
7)

Attributes are one of the main ways we can add interactivity to the pages with htmx.

htmgo provides various methods to add attributes to elements, as well as adding attributes based on a condition.

Copy
1h.Div(
2	h.Class("flex gap-2"),
3	h.Id("my-div"),
4    h.If(
5      error != "",
6	  h.Class("p-4 bg-rose-400 text-white rounded"),
7	)
8)
In this example we are conditionally adding an attribute based on if there is an error on not, you'll learn more about conditionals in the next few pages.

Example using htmx attributes:

Copy
 1h.Tr(
 2	h.Class("flex gap-2"),
 3	h.HxInclude("input")
 4	h.Td(
 5		h.Input("text",
 6			h.Class("p-4 rounded"),
 7			h.Placeholder("Type something"),
 8			h.Name("my-input"),
 9		)
10	),
11   	h.Td(
12		h.Button(
13			h.Text("Submit"),
14		)
15	),
16)