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

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 button
    • open = !open provides the fundamental Boolean logic for switching between true and false states
    • x-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
⬅️ older (in textbook-alpinejs)
🏔️ Starting with Alpine.js
⬅️ older (in code)
🏔️ Starting with Alpine.js
⬅️ older (posts)
🏔️ Starting with Alpine.js