-2

I am creating a invoice software in html, css, and javascript. I fetched with document.getElementById(), but in console is showing null value I searched a lot on internet i found that js file is loading earlier than my html file. Please tell solution.

I tried many times but did not succeed

  • Welcome to StackOverflow. Please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) – M. Yousfi May 17 '23 at 16:14
  • 2
    Probably it's [this often-reported issue.](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Pointy May 17 '23 at 16:15

2 Answers2

0

If you are using an external script, use the defer attribute:

<script src="some_script.js" defer><script>

If you're using an inline script, use type=module:

<script type="module">
    x = document.getElementById("your_id");
    console.log(x);
</script>
code_monk
  • 9,451
  • 2
  • 42
  • 41
-1

you can try it by giving it between the body and the html closing tag so that your html file loads first then the js file.