Encapsulating directives with x-bind and $el #4841
Answered
by
ekwoka
devops-at-alinea
asked this question in
1. Help
Alpine.js version3.5.12 Browser and operating systemChrome on Windows What do you need help with?The $el magic property doesn't seem to work within an encapsulated directive using x-bind: Alpine.data('textControl', () => ({
input : {
['@keyup']() {
console.log($el);
}
},
}));<div x-data="textControl">
<input x-bind="input"/>
</div>caught ReferenceError: $el is not definedHow do you expect it to work?If I can use $el in an event: <button @click="$el.innerHTML = 'Hello World!'">Replace me with "Hello World!"</button>And that event can be bound in an encapsulated directive: Alpine.data('dropdown', () => ({
open: false,
trigger: {
['@click']() {
this.open = ! this.open
},
},
}))```then I would expect it to work. Please confirm
|
Answered by
ekwoka
Jun 8, 2026
Replies: 1 comment
|
So in the JS, $el doesn't refer to anything. It's directly being used as an identifier. binding objects allow string expressions. So you could do Otherwise, I think |
0 replies
Answer selected by
devops-at-alinea
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So in the JS, $el doesn't refer to anything. It's directly being used as an identifier.
binding objects allow string expressions. So you could do
['@keyup']: 'console.log($el)'to directly be able to use the magics.Otherwise, I think
this.$elcould work (feels like it should but I don't immediately remember)