Htmx

From G1Joshi/Agent-Skills · View interactive version

Htmx is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert htmx assistance covering hypermedia-driven UIs, AJAX swaps, out-of-band updates, and SSE/WebSockets. Use when building dynamic web interfaces without writing complex client JavaScript.

How to use this skill

Skill file

htmx

High power tools for HTML. Allows you to build modern user interfaces with the simplicity of hypertext.

When to Use

Quick Start

<script src="https://unpkg.com/htmx.org@1.9.10"></script>

<!-- When clicked, issue GET to /clicked, swap outerHTML with response -->
<button hx-get="/clicked" hx-swap="outerHTML">Click Me</button>

Core Concepts

Declarative AJAX with Trigger & Swap Directives

Making async requests and swapping server HTML directly into DOM targets:

<!-- Instant search input with debouncing -->
<div class="search-widget">
  <input
    type="text"
    name="q"
    placeholder="Search users..."
    hx-post="/api/users/search"
    hx-trigger="keyup changed delay:300ms, search"
    hx-target="#search-results"
    hx-indicator="#loading-spinner"
    class="form-control"
  />
  <div id="loading-spinner" class="htmx-indicator">Searching...</div>
  <div id="search-results">
    <!-- Server returns <ul> list HTML here -->
  </div>
</div>

Inline Editing with Target Swapping

Updating tabular records in-place without page reloads:

<!-- Table Row Component -->
<tr id="user-row-42">
  <td>Alice Johnson</td>
  <td>alice@example.com</td>
  <td>
    <button
      class="btn btn-sm btn-primary"
      hx-get="/users/42/edit"
      hx-target="#user-row-42"
      hx-swap="outerHTML"
    >
      Edit
    </button>
  </td>
</tr>

Out-of-Band (OOB) Updates & Server-Sent Events

Updating multiple disconnected DOM regions from a single response:

<!-- Server response contains main payload and an OOB notification badge -->
<div id="order-details">
  <h4>Order #1029 Confirmed</h4>
  <p>Status: Processing</p>
</div>

<!-- Out-of-band swap updates the navbar cart badge automatically -->
<span id="cart-counter" hx-swap-oob="true" class="badge bg-danger"> 3 </span>

Common Patterns

Inline Search with Debounced Active Search

Problem: Building responsive autocomplete search typically requires heavy React/Vue state machines.

Solution:

Use declarative htmx attributes on standard HTML input:

<input
  type="search"
  name="q"
  placeholder="Search users..."
  hx-post="/api/users/search"
  hx-trigger="keyup changed delay:300ms, search"
  hx-target="#search-results"
  hx-indicator="#loading-spinner"
/>

<div id="loading-spinner" class="htmx-indicator">Searching...</div>
<div id="search-results"></div>

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------ | :---------------------------------------------------------------- | :------------------------------------------------------------------------------- |

| htmx:targetError: Target element does not exist | Element specified in hx-target ID selector is missing from DOM. | Verify ID in DOM: #search-results must exist before request triggers. |

| Response received but nothing swapped | Server returned empty response or non-200 HTTP code. | Verify server returns HTML with HTTP 200, or configure hx-swap response codes. |

| Infinite request loop | hx-trigger="load" placed inside swapped HTML fragment. | Move load trigger to outer persistent container or remove trigger on swap. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.