Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

262
Visualizações
Trying to create a function that changes the text of an html file

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>

So my goal here is to change the contents of temp1 to temp3 using js, however the above function is not working, the image not being present right now is not an issue. I would appreciate any guidance.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

First you need to call the method, and it's not getElementByClassName it's getElementsByClassName.

And chump.length is not a function.

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>

about 4 years ago · Juan Pablo Isaza Relatório

0

You misspelled

document.getElementByClassName("first-txt");

Change it to :

document.getElementsByClassName("first-txt");

And chump.length not chump.length()

Code same as :

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>

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this:

function chumpage() {
  var chump = document.getElementsByClassName('first-txt');
  for (var i = 0; i < chump.length; i++) {
    chump[i].innerText = "temp3";
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda