Hiding and showing content in Alpine.js
toggling stuff with this simple JavaScript framework
2025-09-18 09:45
// updated 2025-09-18 12:16
// updated 2025-09-18 12:16
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