Understanding $el, $refs, and $root
$el
The Current Canvas
The element you are actively working on.
$refs
Named Supplies
Specific tools you grab by name.
$root
Entire Workspace
The whole studio component area.
$el refers to the element a directive is on. Here, the button can change its own properties, like an artist making a mark on the canvas they are touching.
<!-- The button modifies itself using $el -->
<button @click="$el.textContent = 'Modified!';
$el.disabled = true;">
Work on this Canvas
</button>
$refs lets you access other elements by name. Give an element a name with x-ref, then access it via this.$refs.name.
<!-- Defining a named supply -->
<div x-ref="bluePaint">Blue</div>
<!-- A button that accesses the named supply -->
<button @click="$refs.bluePaint.classList.add('selected')">
Select Blue Paint
</button>
$root refers to the root element of the component (the one with x-data). It's useful for actions that affect the entire component.
The whole studio has a new vibe!
<!-- The root component element -->
<div x-data="{...}">
<!-- A button anywhere inside -->
<button @click="$root.classList.add('new-look')">
Change Workspace
</button>
</div>
| Property | Analogy | Scope | Common Use Case |
|---|---|---|---|
| $el | Current Canvas | The exact HTML element the directive is on. | Modifying the element that triggered an event. |
| $refs | Named Supplies | Any element with an x-ref attribute within the component. |
Accessing other specific elements from an event handler. |
| $root | Entire Workspace | The component's top-level element (with x-data). |
Affecting the entire component's state or appearance. |