Explore these core Alpine.js concepts through the analogy of organizing a closet. See how each "magic property" targets different parts of the whole.
The Current Outfit Piece
$el refers to the current DOM element. It's like an item of clothing knowing about itself. An action on the element directly affects that same element.
Try it:
<button @click="$el.classList.toggle('styled')">
This Shirt
</button>
Named Storage Areas
$refs lets you access other specific elements within your component by name. It's like a control panel to organize items in pre-defined, named storage areas.
The Closet:
Control Panel:
<div x-ref="shoeRack">...</div>
<button @click="$refs.shoeRack.focus()">
Select Shoes
</button>
The Entire Closet
$root refers to the component's top-level element (the one with x-data). It's like an action that affects the entire closet structure, not just one piece or one area.
Try it:
<div x-data="...">
<!-- This is the $root element -->
<button @click="$root.classList.add('tidy')">
Tidy Closet
</button>
</div>