--- name: alpine description: | Alpine.js is a lightweight JavaScript framework for adding interactivity to HTML markup. It provides reactive data, event handling, and DOM manipulation through HTML attributes — like a modern jQuery replacement with declarative syntax. license: Apache-2.0 compatibility: 'Alpine.js 3.17.x; any web server or bundler; modern browsers' metadata: author: terminal-skills version: 1.1.0 repository: https://github.com/alpinejs/alpine category: development tags: "javascript, html, lightweight, reactive" --- # Alpine.js ## Overview Alpine.js adds reactive behavior directly in HTML markup using `x-` attributes. It's ideal for adding interactivity to server-rendered pages without a build step or SPA framework. The current release is 3.17.5 (npm, October 2026); all snippets below were run against it in a DOM test. ## Instructions Load Alpine with `defer`, put `x-data` on the element that owns the state, and use directives (`x-show`, `x-text`, `x-model`, `x-for`, `@event`) inside it. Reusable logic goes into `Alpine.data()`, shared state into `Alpine.store()`. ## Installation ```html ``` ```javascript // main.js — npm module setup import Alpine from 'alpinejs'; window.Alpine = Alpine; Alpine.start(); // call once, after registering Alpine.data / Alpine.store / plugins ``` If the page runs under a strict Content-Security-Policy without `unsafe-eval`, use the `@alpinejs/csp` build, which does not use `eval`/`new Function` and so limits what inline expressions may contain. ## Core Directives ```html

This content can be toggled

Count:

``` ## Data Binding ```html

Hello, !

Colored text
Dynamic styles
``` ## Loops ```html
``` ## Event Handling ```html
Count:
``` ## Component Patterns ```html
``` ## Reusable Data with Alpine.data ```html

``` ## Working with htmx ```html

Article Title

``` ## Stores (Global State) ```html
Click me
``` ## Magic Properties ```html
``` ## Examples **"Add a dropdown that closes when I click elsewhere."** Use the dropdown above: `@click.outside="open = false"` on the wrapper and `@keydown.escape.window` on the menu. Result: the menu toggles on click, closes on outside click or Escape. **"Search as I type without a framework."** `` calls `search` 300 ms after the last keystroke; define `search` in `Alpine.data('searchBox', ...)` and use `x-data="searchBox"`. ## Guidelines - Use `x-data` on a parent element to define reactive scope — everything inside shares that state - Use `x-show` for toggling visibility (CSS), `x-if` for conditional DOM insertion - Use `x-model` for two-way binding on inputs, selects, checkboxes - Use event modifiers (`.prevent`, `.stop`, `.debounce`, `.outside`) to reduce boilerplate - Use `Alpine.data()` to extract reusable components with methods and computed properties - Use `Alpine.store()` for global state shared across components - Pairs excellently with htmx: Alpine handles UI state, htmx handles server communication - Give `x-for` a unique `:key` (an id), not the index, when items can be removed or reordered; `x-for` and `x-if` must sit on a `