How to access data and getters from an Alpine component with getElementById()
?
#2413
Unanswered
ralphjsmit
asked this question in
1. Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey Alpine team, thanks for making and maintaining this great tool!
Today I've a question about using a document.getElementById() or a dynamic
x-ref
and I'm really hoping you could help me with that.In short, I'm trying to create a map of a barge / ship. On this map, users should be able to select a location for machines on the ship themselves. See the image below. The dark gray parts are the locations of existing machines, the blue rectangle the current selection and the red squares the parts where the selection overlaps existing machines. Must say that I'm quite proud of it already as someone who doesn't have much feeling with JS.
Nevertheless, there's still something I cannot get fixed. That has to do with the red squares, where the selection overlaps already existing machines. As you probably might imagine, any selection that overlaps existing machines is not valid.
I'd like to disable making a selection if part of the selected area is disabled.
I can reference each individual element with this:
The problem is that
var el = document.getElementById('' + index + i);
returns the correct element, but that I can't doel.inSelectionRange
orel.disabled
in order to access the Alpine x-data.How should I change this code, so that I can reference/use the Alpine data?
Here is the Codepen with the example
The inserted data from PHP looks like this (I hardcoded it for the codepen):
I'd would really appreciate it if you could take a look and explain me how to retrieve the correct DOM element and how I can use the data from Alpine.
Thanks!
Beta Was this translation helpful? Give feedback.
All reactions