Hiding and showing content with Alpine.js
toggling stuff with this simple JavaScript framework
// updated 2025-09-19 09:39
Showing and hiding (toggling) content in Alpine.js makes use of simple Boolean logic:
Breaking that down:
x-data
contains a single Boolean state variable,open
- true if the content is shown
- false if the content is hidden
x-on:click
listens for a click on the buttonopen = !open
provides the fundamental Boolean logic for switching between true and false statesx-on:click.outside
allows that "click anywhere to make it disappear" functionality
x-show
does the conditional rendering- depends on the
open
state variable declared in x-data
- depends on the