-
htmx is still another JS lib. So if you hate JS downright from the beginning maybe you should look for something else then :kidding:.
-
With htmx we're still making dynamic projects.
-
In htmx we focus on hypermedia and utilizing it to build our apps.
-
In HTML we have
a
andform
elements which support hypermedia:-
<h1>The First Page</h1> <p> If you like, you can switch to the <a href="http://www.dr-chuck.com/page2.htm">Second Page</a> </p>
-
<form action="http://example.com/register" method="post"></form>
But aside from those two we do not have any other option. Here is where htmx enters the scene. With a flick of its wand and a murmur of ancient words htmx enables us to use hypermedia in almost every element.
-
-
It adds new html attributes to elements:
hx-*
. -
It expects server to return hypermedia and not
JSON
. -
For it to work we need to add a
script
tag to our HTML document. This can be done in two ways:- Through their CDN.
- Download the minified version and link it, which the best way to do it in real world apps.
— Ref.
- Triggers a get request to the specified URL.
- When the response comes back where htmx should put it will be dictated by this custom htmx attribute:
- Can use any kind of css selector:
.some-class
,#some-id
, or something like.class-name > p:nth-child(3)
.
- This tells how it should swap the content with the response:
innerHTML
: Swaps the inner HTML of the element with the response.outerHTML
: Swaps the the entire element with the response.