Next.js Discord

Discord Forum

document.getElementById returns null, not sure how to fix the issue

Unanswered
Netherland Dwarf posted this in #help-forum
Open in Discord
Netherland DwarfOP
Hey,

So I'm trying to build a simple mouse tracker that should only track the pointer when inside a specific container. I have assigned an id to the container (say 'container' for the example), have declared a variable (say 'trackerContainer') as = document.getElementById('container') and am trying to track the pointer using container.onmousemove = (event) => {}. However I'm getting an error message saying that 'trackerContainer' returns null. I've tried using useRef() and attach a ref to the container but didn't fix the issue. I can't find the solution to fix the issue, please help 🙂

1 Reply

American Crow
Const myref = useRef(null) and giving the container the ref is the right way. You most likely forgot to call myRef.current. Provide some code if you want better help