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

162
Visualizações
Replace identical nested divs within different outer div from javascript

I have this problem which I am trying to solve after I cloned a div multiple times using javascript. I want to replace the inner div with an empty div having a particular id for each. Assuming I have:

<div id="original">
    //some code here
        <div id="problem">
            //some code here
        </div>
    </div>

<div id="location">
//some code here
    <div id="problem">
        //some code here
    </div>
</div>

<div id="rep">
//some code here
    <div id="problem">
        //some code here
    </div>
</div>

I need to be able to get the div with id problem in the div location and replace it with something else and same to the div within the div repository.

I tried

document.getElementById("location") but that returns the whole content and I cannot replace a particular div within it and if I try document.getElementById("problem") I cannot specify which one I need

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

0

you can use querySelector on a tag to get the first element that match a selector

you can combine it with

  • innerText / innerHTML to modify directly content
  • appendChild if you have create an html structure in js part

var locationDiv = document.getElementById('location');
var problemDiv = locationDiv.querySelector('#problem');
problemDiv.innerText = 'test';
<div id="original">
        <div id="problem">
        </div>
    </div>

<div id="location">
    <div id="problem">
    </div>
</div>

<div id="rep">
    <div id="problem">
    </div>
</div>

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