+ 2
script won't work
<body> <p>Paragraph</p> <script> function change() { var elem = getElementByTagName("p") elem.innerHTML="Changed" } setTimeout(change, 1000) </script> </body>
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
0
<head>
<script>
function change() {
document.getElementById("p").innerHTML = "Changed!";
}
setTimeout(change, 1000);
</script>
</head>
<body>
<p id="p">Paragraph</p>
</body>
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>
- 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>
- 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.