I try to run script from VS code , it always show empty nodelist but when I run with chrome console it works properly. Please give me some advice if I’m doing it wrong.
How are you running the script in the HTML, when does the code run? Is the DOM ready?
Try wrapping the code inside a DOMContentLoaded handler.
I try to use DOM method to the webpage.
This is how I linked js file to the HTML file(It’s in the same folder). When I run on local or on server (with vscode) it looks like js file doesn’t link on HTML file but when I write the same code on developer tool console it works fine.
</body> ends like this:
<body> <!-- HTML --> <script src="app.js"></script> </body>
Another solution is to add the
defer attribute inside the script tag, so it will run after the DOM is loaded. like this:
<script defer src="app.js"></script>
Hope this helps
It works! Thank you very much. I have one more question.
Does the place of the script matter? For example : if I want to run script after the
I need to put it after that not on the head.
Thank you very much.