Help! Dont understand how to get my external javascript to function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help! Dont understand how to get my external javascript to function

I am trying to use the length property of the array to loop through all the selected elements. basically run through and change all <p> tag content to "Hi there". I don't understand how to make the JavaScript work externally. ..................."NewDemo.html".............................. <!DOCTYPE html> <html> <head> <title>External javaScript</title> </head> <body> <p>hey</p> <p>hi</p> <p>hello</p> <script src="NewDemo.js"> </script> </body> </html> ...................................External JavaScript.................................... ................."NewDemo.js"............................. var arr = document.getElementsByTagName("p"); for(var x=0;x<arr.length;x++){ arr[x].innerHTML = "Hi there"; }

1st Mar 2017, 1:35 AM
Sushi_Theif
Sushi_Theif - avatar
8 Answers
+ 2
I just recreated your code just as I explained and it works flawlessly. I will suggest you restart your text editor and possibly delete the working folder. Start the editor again and create new folder and add your "NewDemo.html" and "NewDemo.js" files recode.
1st Mar 2017, 1:43 AM
Benneth Yankey
Benneth Yankey - avatar
+ 1
glad it works, happy coding!
1st Mar 2017, 1:52 AM
Benneth Yankey
Benneth Yankey - avatar
0
The script source is pointing to "javaPractice.js", so create a new file named as source and put in the javascript code. Also make sure the html and the javascript files are in the same root folder
1st Mar 2017, 1:24 AM
Benneth Yankey
Benneth Yankey - avatar
0
I started over and named the Html file "NewDemo.html" and I named the javaScript "NewDemo.js. I also change my script tags for it to look for "NewDemo.js". They are in the same root folder as well. However I am getting this error still "Failed to load resource: net::ERR_FILE_NOT_FOUND - NewDemo.js"
1st Mar 2017, 1:32 AM
Sushi_Theif
Sushi_Theif - avatar
0
Ill update my first message
1st Mar 2017, 1:33 AM
Sushi_Theif
Sushi_Theif - avatar
0
the debbuger tool wont even show my external javascript code. its just a blank page even though I have saved my code
1st Mar 2017, 1:37 AM
Sushi_Theif
Sushi_Theif - avatar
0
Yeah I just got it working aswell. So something veeerrry strange was happening. For what ever reason every time I would save my "NewDemo.html" it would overwrite "NewDemo.js" and vice-verse. Basically they were deleting one another every time I would try and save them. So, I shut everything down and made a complete new folder and saved them both within it and tried running "NewDemo.html" and suddenly it works. Do not understand why saving my html file would delete and overwrite my js file. That is new to me.
1st Mar 2017, 1:51 AM
Sushi_Theif
Sushi_Theif - avatar
0
Yep, lol thanks for letting me know I'm not an idiot and some other phenomena is the culprit for my code not working :p
1st Mar 2017, 1:53 AM
Sushi_Theif
Sushi_Theif - avatar