function chumpage() { chump = document.getElementByClassName("first-txt"); for (var i = 0; i < chump.length(); i++) { chump[i].innerText = "temp3"; } } <head> <style> .temp { margin: 3%; position: relative; } .first-txt { position: absolute; top: 17px; left: 50px; } .second-txt { position: absolute; bottom: 20px; left: 10px; } </style> </head> <body> <div class="temp"> <img src=""> <h3 class="first-txt"> Temp1 </h3> <h3 class="second-txt"> Temp2 </h3> </div> </body> </html>Entonces, mi objetivo aquí es cambiar el contenido de temp1 a temp3 usando js, sin embargo, la función anterior no funciona, la imagen que no está presente en este momento no es un problema. Agradecería cualquier orientación.
Primero debe llamar al método, y no es getElementByClassName , es getElementsByClassName .
Y chump.length no es una función.
function chumpage() { const chump = document.getElementsByClassName("first-txt"); for (var i = 0; i < chump.length; i++) { chump[i].innerText = "temp3"; } } chumpage(); <head> <style> .temp { margin: 3%; position: relative; } .first-txt { position: absolute; top: 17px; left: 50px; } .second-txt { position: absolute; bottom: 20px; left: 10px; } </style> </head> <body> <div class="temp"> <img src=""> <h3 class="first-txt"> Temp1 </h3> <h3 class="second-txt"> Temp2 </h3> </div> </body> </html>escribiste mal
document.getElementByClassName("first-txt");
cambiarlo a:
document.getElementsByClassName("first-txt");
Y chump.length no chump.length()
Código igual que:
function chumpage() { chump = document.getElementsByClassName("first-txt"); for (var i = 0; i < chump.length; i++) { chump[i].innerText = "temp3" } } chumpage() .temp { margin: 3%; position: relative; } .first-txt { position: absolute; top: 17px; left: 50px; } .second-txt { position: absolute; bottom: 20px; left: 10px; } <div class="temp"> <img src=""> <h3 class="first-txt"> Temp1 </h3> <h3 class="second-txt"> Temp2 </h3> </div>Prueba esto:
function chumpage() { var chump = document.getElementsByClassName('first-txt'); for (var i = 0; i < chump.length; i++) { chump[i].innerText = "temp3"; } }