Is it possible to access x-bind directive DOM element from component? #4283
Answered
by
SimoTod
arthurshlain
asked this question in
1. Help
-
Hi. The question is how to access DOM element without using any additonal querySelector when For example: <div x-data="dropdown()">
<button x-bind="trigger">Open Dropdown</button>
<span x-bind="dialogue">Dropdown Contents</span>
</div> document.addEventListener('alpine:init', () => {
Alpine.data('dropdown', () => ({
open: false,
...
dialogue: {
['x-show']() {
return this.open
},
...
},
getDialogueHeight(){
// need something like that
//return this.dialogue.$el.clientHeight()
// instead of weird:
retrn this.$root.querySelector('[x-bind="dialogue"]').clientHeight;
}
}))
}) Or it's impossible because the one directive can be applied to many DOM elements? |
Beta Was this translation helpful? Give feedback.
Answered by
SimoTod
Jul 1, 2024
Replies: 1 comment
-
In your bindings, add x-ref, then you can use See https://github.com/alpinejs/alpine/blob/main/packages/ui/src/combobox.js#L288 for an example |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
arthurshlain
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In your bindings, add x-ref, then you can use
this.$refs.dialog.getClientHeight()
.See https://github.com/alpinejs/alpine/blob/main/packages/ui/src/combobox.js#L288 for an example