#what is the the best way to access a document Element

6 messages · Page 1 of 1 (latest)

low thicket
#

Hi guys what is the best approach to do dom manipulation in astro?

for example i want to access a div with a specific id

vivid stag
low thicket
#

for example something like this ?

import {useEffect} from "preact/hooks";
import { h } from "preact";
export function getStaticPaths(): string {
  let text: string = "";
   useEffect(() => {
      //@ts-ignore
     text  = document.getElementById("test").innerText;
   
  });
return text;
}
brave pumice
#

isn't that good enough ? Would you expect an answer that involves a particular third party framework, or is your question intended for pure Astro ?
There's also the querySelector() and querySelectorAll() functions
https://github.com/MicroWebStacks/astro-big-doc/blob/b3600c5dd0a6bf4e75e1d1a58ae19a9826c82d05/src/components/panzoom.astro#L135

Also an important point in Astro, if you use client side js, you might need a way to sync usage of multiple instances of the same component. What I do is I create a unique id that I use to select in the query, not sure if Astro should handle/obfuscate such thing in the future or stay as pure and as lightweight as its original intentions

const container = document.querySelector(`.container[data-uid="${uid}"]`)
low thicket
brave pumice
# low thicket Sorry maybe I should have detailed my Problem more. I want to add a jwt query t...

hmm, your explanation is even more intriguing. If I would think about it at meta level, I would say, try to do it with vanilla js and make it work even if the code is ugly, at least you understand it, and you'd have a code basis to share with a link if it does not work. Once there, you can think of optimization on second step by adopting a framework or refactoring your routines. Anything that is not clear and cannot be debugged will either not work or suprise you with bugs 😉
I'm not a front end experts but if you have a link to a non working project, I'd give it a try, I'm also learning jwt at the moment and would like to add github authentication, which porbably does not need jwt, but jwt might be handing to protect some apis some times.