+ 2

script won't work

<body> <p>Paragraph</p> <script> function change() { var elem = getElementByTagName("p") elem.innerHTML="Changed" } setTimeout(change, 1000) </script> </body>

10th Nov 2016, 1:18 PM
koder
koder  - avatar
5 Réponses
+ 1
I was able to fix it like this: // The code starts here ---> <html> <head> </head> <body> <div id="firstDiv"> <h1>Hello World!</h1> <p>Here we have some text.</p> <p>Here we have more text.</p> <p>And here we have even more text.</p> </div> <script> var myElements = document.getElementsByTagName("p"); var arrElements = myElements; for (x = 0; x < arrElements.length; x++) { arrElements[x].innerHTML = "Change"; } </script> </body> </html> // The code ends here <--- You were missing some ";", but even with them, the code was unable to run. So I've added few things, and now it's okay. You can place it inside a function and make it work with setTimeout or setInterval. I hope this is useful. You can test the working code here: https://code.sololearn.com/W783mbBH781t/#html
15th Dec 2016, 7:11 PM
unknown
0
<head> <script> function change() { document.getElementById("p").innerHTML = "Changed!"; } setTimeout(change, 1000); </script> </head> <body> <p id="p">Paragraph</p> </body>
10th Nov 2016, 1:25 PM
Fronz-Tec
Fronz-Tec - avatar
0
This works fine for me: <html> <head> <title>Page Title</title> <script> function change() // <-- just forgot the brackets ;) { document.getElementById("p").innerHTML= "Changed"; } setTimeout(change, 1000); </script> </head> <body> <p id="p">Paragraph</p> </body> </html>
10th Nov 2016, 5:23 PM
Pink Lemonade
- 2
Still doesn't work I wrote <!DOCTYPE html> <html> <head> <title>Page Title</title> <script> function change { document.getElementById("p").innerHTML= "Changed"; } setTimeout(change, 1000); </script> </head> <body> <p id="p">Paragraph</p> </body> </html>
10th Nov 2016, 2:10 PM
koder
koder  - avatar
- 3
I think the problem is that your <p> has not been loaded when your function happens. If the function was in your body it should work.
12th Nov 2016, 2:37 AM
Greyson Naugle
Greyson Naugle - avatar