HTMX Crash Course | Dynamic Pages Without Writing Any JavaScript

Traversy Media

Traversy Media

56 min, 47 sec

A comprehensive guide to using HTMX, a small JavaScript library for creating dynamic front-ends without writing JavaScript.

Summary

  • HTMX allows the creation of dynamic front-ends using hyperscript attributes in HTML to make HTTP requests.
  • Using HTMX, we can handle all HTTP methods including GET, POST, PUT, PATCH, and DELETE directly within HTML.
  • Examples include making HTTP requests with buttons, real-time search functionality, inline form validation, and polling for updated data.
  • HTMX is compatible with any backend and is particularly popular with Django, but also works well with Node.js and Express.
  • The demonstration includes setting up a simple server with Node.js and Express, and building small projects to showcase HTMX's capabilities.

Chapter 1

Introduction to HTMX

0:00 - 4 min, 1 sec

Introduction to HTMX for creating dynamic front-ends with hyperscript attributes without writing JavaScript.

Introduction to HTMX for creating dynamic front-ends with hyperscript attributes without writing JavaScript.

  • HTMX is a small JavaScript library for building dynamic front-ends.
  • It leverages special hyperscript attributes in HTML to make HTTP requests directly.
  • HTMX enables using all HTTP methods including GET, POST, PUT, PATCH, and DELETE in HTML forms.
  • HTTP requests can be triggered using any element, not just anchor tags and form tags.
  • HTMX is lightweight at only 14 kilobytes and is ideal for full-stack applications.

Chapter 2

Setting Up a Basic Server with Node.js and Express

3:41 - 2 min, 35 sec

Setting up a basic server using Node.js and Express for serving HTML files.

Setting up a basic server using Node.js and Express for serving HTML files.

  • A public directory is created to serve HTML files where HTMX attributes will reside.
  • Node.js and Express are used for the backend server, listening on port 3000.
  • Middleware setup allows serving static files and handling JSON or form data.
  • Nodemon is used in development to watch the server file and restart on changes.

Chapter 3

Making HTTP Requests with Buttons Using HTMX

6:17 - 6 min, 10 sec

Demonstration of making HTTP requests with buttons using HTMX attributes.

Demonstration of making HTTP requests with buttons using HTMX attributes.

  • Buttons are set up to make HTTP POST requests to specific endpoints using the 'hx-post' attribute.
  • Different HTTP methods can be used, and responses can be swapped into the page without a reload.
  • Using 'hx-swap', the response from the server can replace the button's content.
  • HTMX 'hx-target' attribute can be used to direct the response to a specific part of the page.
  • A backend route is created to handle requests and return dynamic data.

Chapter 4

Real-Time Search Functionality Using HTMX

12:27 - 34 min, 25 sec

Implementing a real-time search functionality using HTMX to make dynamic requests while typing.

Implementing a real-time search functionality using HTMX to make dynamic requests while typing.

  • A form input is used to trigger 'hx-post' requests to a '/search' endpoint with each keystroke.
  • The response target is set to update a specific area of the page with search results.
  • The server-side route performs the search operation and returns HTML with the results.
  • The demonstration shows search results being updated in real-time as the user types.

Chapter 5

Inline Form Validation Using HTMX

46:53 - 9 min, 55 sec

Creating inline form validation that triggers on input change to validate an email address using HTMX.

Creating inline form validation that triggers on input change to validate an email address using HTMX.

  • An input field is set up to make a 'hx-post' request to the server for validating an email address.
  • HTMX updates the form input field with validation messages without reloading the page.
  • The server-side route validates the email format and returns corresponding messages and styles.