Learn the minimal, HTML-friendly JavaScript framework for reactive UIs
Install Alpine.js in your project using one of the following methods:
<script src="https://unpkg.com/alpinejs" defer></script>
npm install alpinejs
Declares reactive JavaScript object on an element. Acts as the component's state.
<div x-data="{ count: 0 }"> <button x-on:click="count++">Increment</button> <span x-text="count"></span> </div>
Attaches event listeners to DOM elements for Alpine.js expressions.
<button x-data="{ open: false }" x-on:click="open = !open"> Toggle </button>
Conditionally shows/hides elements based on reactive expressions.
<div x-data="{ showContent: true }"> <button x-on:click="showContent = !showContent">Toggle</button> <div x-show="showContent"> This content toggles visibility. </div> </div>
Reactivity for DOM properties, classes, attributes, and styles.
<div x-data="{ isActive: true }"> <button x-bind:class="{ 'bg-blue-500 text-white': isActive }">Dynamic Class</button> </div>
Cross-directional data binding for form elements like inputs and selects.
<div x-data="{ name: 'Alpine' }"> <input x-model="name" placeholder="Enter your name" /> <span x-text="name"></span> </div>
Conditionally apply classes using Alpine's reactive expressions.
<div x-data="{ isPrimary: true }"> <button x-class="{ 'bg-blue-500 text-white': isPrimary }"> Primary Button </button> </div>
Seamlessly animate elements showing/hiding with Alpine's built-in animation system.
<div x-data="{ open: false }"> <button @click="open = !open">Open</button> <div x-show.transition.out="open">Animated</div> </div>
Trigger logic when specific reactive properties change using $watch()
<div x-data="{ count: 0, init() { this.$watch('count', value => { console.log('Count changed to', value); }); } }"> <button @click="count++">Increment</button> <span x-text="count"></span> </div>
Native HTML form validation combined with Alpine expressions
<form x-data="{ email: '', errors: {} }"> <input type="email" x-model="email" required> <template x-if="!email.endsWith('@example.com')"> <div class="text-red-500 text-xs mt-1"> <span>Please provide a valid email</span> </div> </template> </form>
Access special Alpine properties in your reactive expressions
<div x-data="{ name: 'Alpine' }"> <p>Initialized at: @elapsed</p> <p>Memory ID: $id('my-component')</p> </div>
Maintain side effects as data changes with Alpine's reactive system
<div x-data="{ count: 0 }"> <p>Count: @count</p> <template x-effect="{ document.title = `Counter: ${count}` }"></template> <button @click="count++">Increment</button> </div>
Use Alpine's API to create reusable components programmatically
document.addEventListener('alpine:init', () => { Alpine.data('myComponent', () => ({ message: 'Hello from Alpine', show: false, init() { this.show = true; } })); }); // In HTML: <div x-data="myComponent()"> <p x-show="show">{{ message }}</p> </div>
This interactive demo uses Alpine.js directly in your browser. Toggle the theme switch or click the button to see reactivity in action!