An Analogy of a School Classroom
To understand these core Alpine.js concepts, imagine our component is a classroom:
$el refers to the current element. Here, clicking a student's button only affects that specific student.
<button @click="raiseHand($el)">...</button>
$refs lets you access any named element from your component's logic. Here, the "Teacher's Desk" can activate specific learning areas by name.
Idle
Idle
<div x-ref="readingCorner">...</div>
<button @click="activateArea('readingCorner')">...</button>
// JS: this.$refs.readingCorner.classList.add(...)
$root refers to the top-level element of your component. It's perfect for actions that affect the whole component, like a class-wide announcement.
<div x-data="classroom" :class="{ '...': isBellRinging }">
<button @click="soundBell()">...</button>
</div>
// JS: this.isBellRinging = true
No activity yet...