+ 2

External Js file not working for selecting elements!

I have 2 files, one html and another javascript file. I m trying to use getElement method in js file to select element present in html file. But it is not working. In HTML: <script src = 'script.js'> </script> <p>hi</p> <p>hello</p> <p>hi</p> ---------------------------- In script.js: var arr = document.getElementsByTagName("p") for (var x = 0; x < arr.length; x++) { arr[x].innerHTML = "Hi there"; } -------------------- It doesn't work!

12th Jun 2020, 3:42 AM
Gaurav Giri
Gaurav Giri - avatar
3 Respostas
+ 5
Always set your dom access JavaScript script tag at the last element of the html document.
12th Jun 2020, 6:30 AM
Calviղ
Calviղ - avatar
+ 5
Is it not working at all or is it giving error? Link your code please.
12th Jun 2020, 5:11 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 2
🤔 should work, can you post your project here?
12th Jun 2020, 3:50 AM
Sandra Meyer
Sandra Meyer - avatar