How to access document in Qwik js. Getting document of undefined when trying to use document.getElementBtId
in a component$
Asked
Active
Viewed 222 times
0

Omair Nabiel
- 1,662
- 2
- 12
- 24
-
Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) so that we can also see the problem and help troubleshoot. – Jake Mar 26 '23 at 17:37
1 Answers
3
You can access to the document object only in browser client side.
You have to use it in a useVisibleTask function which is called when document becomes visible in browser.
export const ExampleComponent = component$(() => {
useVisibleTask$(() => {
// Only runs in the client
const element = document.getElementById("your-id")
});
return <div id="your-id">Example component</div>;
});

Constant Gillet
- 106
- 5