#document.querySselector('.data-include') keeps returning null even tho its right there

27 messages · Page 1 of 1 (latest)

hexed finch
#

pls @ me when u reply
brb i gotta step afk for a sec

robust anchor
#
document.querySelector("[data-include]").setAttribute('data-include', 'test')

console.log(document.querySelector("[data-include]"))```
fossil seal
robust anchor
#

I think it's not a good idea to use the same name for a class and a data-attribute

fossil seal
#

Either there is a problem with using the same class name and attribute name, or you have other code that is modifying the DOM in unexpected ways.

robust anchor
#

and a shorter way to set data-attributes:

test.dataset.include = 'newTest'```
fossil seal
# hexed finch pls @ me when u reply brb i gotta step afk for a sec

I noticed that you have two different scripts and one of them has a comment indicating that it loads in the content. Is the HTML that you showed us a modified example or is that really in your actual code? I am wondering if you just showed that to us in the HTML for simplicity but it is actually generated by the JS.

hexed finch
fossil seal
hexed finch
fossil seal
fossil seal
hexed finch
#

also this is the htmlinclude.js file (found it on github its the only onee ive found so far that lets the div data-include='file.html' thing work) https://pastebin.com/NVvX9Lv4

hexed finch
#

inspector*

fossil seal
hexed finch
fossil seal
#

Why are you even using this obscure technique? You could use React or Astro to achieve something similar. These are far more popular and well tested approaches.

hexed finch
#

and the class im in doesnt teach react or astro and ive never used those before

fossil seal
#

I read it again and see that the script code is separate from the outerHTML. It really is modifying elements on the page. It only modifies when an element is in the viewport. You might have seen the elements still existing in dev tools because you were looking at code for elements that had not scrolled into view yet.

hexed finch
#

but even after scrolling everything into view (almost everything is in view on most pages) it still says null

fossil seal
hexed finch
#

i checked the page source and it doesnt change but i just checked the web inspector and ya it does replace the div

#

so ya i just figured it out but thx anyway

#

gtg