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

212
Visualizações
How to append an element to a div with className that have a child elemnt with idName

MY html has multiple divs that share the same class name and they have input elements with unique ids, I want to append a span under all of these inputs with JavaScript without editing the html.

this is the part of html

<form name="form" onsubmit="return validate();" method="get">
   <div class="form-group">
      <label for="pass">Password</label>
      <input type="password" class="form-control" name="pass" id="pass">
   </div>
   <div class="form-group">
      <label for="pass2">Re-Type Password</label>
      <input type="password" class="form-control" name="pass2" id="pass2">
   </div>
.
.
.
</form>

this my JS to create and append the span

const errorMessage = document.createElement('span');
errorMessage.textContent = "invalid input";
document.querySelector('.form-group').appendChild(errorMessage);

But this only creates one span in the first div

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

0

you should get all items has class form-group with querySelectorAll

const arrayElement = document.querySelectorAll('.form-group');

for(let i = 0; i < arrayElement.length; i++)
{
 const errorMessage = document.createElement('span');
 errorMessage.textContent = "invalid input";
 arrayElement[i].appendChild(errorMessage);
}

more : https://jsfiddle.net/7znf685q/2/

about 4 years ago · Juan Pablo Isaza Relatório

0

Use querySelectorAll() and iterate.

querySelectorAll(".form-group").forEach(form => {
    const errorMessage = document.createElement('span');
    errorMessage.textContent = "invalid input";
    form.appendChild(errorMessage);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

querySelector just gets the first instance. You need to use querySelectorAll. Then put everything inside a loop

let group = document.querySelectorAll('.form-group') 
 
for (let i = 0; i < group.length; i++) {
  const errorMessage = document.createElement('span');
  errorMessage.textContent = "invalid input";
  group[i].appendChild(errorMessage);
}
<form name="form" onsubmit="return validate()" method="get">
  <div class="form-group">
    <label for="pass">Password</label>
    <input type="password" class="form-control" name="pass" id="pass">
  </div>
  <div class="form-group">
    <label for="pass2">Re-Type Password</label>
    <input type="password" class="form-control" name="pass2" id="pass2">
  </div>
</form>

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