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

233
Visualizações
JQuery , adding div dynamically with error message

I am getting the below json response from an validation api , the first attribute is the id of each input field with the error message.

{
    "backgroundcolor": "Color is not Supported",
    "terms" : "Sorry the Terms are blank"
}

I am using jquery and bootstrap and want to append the below html just after each input field of that specific id . Is this possible to add div dynamically ?

<div id="backgroundcolor" class="invalid-feedback">
  Color is not Supported
</div>

Before :

<div class="col-md-6">
    <input type="text" class="form-control is-invalid" id="backgroundcolor" aria-describedby="validationServer03Feedback" required>
</div> 

After :

<div class="col-md-6">
<input type="text" class="form-control is-invalid" id="backgroundcolor" aria-describedby="validationServer03Feedback" required>
<div id="backgroundcolorFeedBack" class="invalid-feedback">
  Color is not Supported.
</div>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

JavaScript alone is the best and fastest solution, so here it is how i would do it:

let response = JSON.parse(response);

let fragment = document.createDocumentFragment()
let child = document.createElement('div');
child.id = Object.keys(response)[0] + 'FeedBack';
child.className = 'invalid-feedback';
child.innerText = Object.values(response)[0];
fragment.appendChild(child);

document.getElementById(Object.keys(response)[0]).after(fragment);

I hope it'll help.

Note: createDocumentFragment() may be slightly faster than createElement alone, but it's not required.

Edit: you can also check if there is already div with that error, it would prevent duplication of divs

if(!document.getElementById(Object.keys(response)[0].length) {
    // Here goes code from above
}
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