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

185
Visualizações
replace nested html elements with javascript

I hava a huge dom element which i should replace some nested list html element in it. I also tried to remove the old nested element and add the new nested element but this also wasnt success.

const someNewNestedListElement = someNestedListElement
const replacedElement= elementToBeReplaced.replaceChild(someNewNestedListElement , elementToBeReplaced.querySelector(".someOldNestedListElement");

The dummy code above runs into an error. Can someone help me out here? Im pretty new to javascript and dom manipulation so i maybe need some tricks here.

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

0

Try out this

const h2 = document.querySelector("h2"),
      button= document.querySelector("button"),
      newItem = document.createElement('p');

newItem.innerText = "replaced text";

button.onclick = function() {
try {
  h2.parentNode.replaceChild(newItem, h2);
  }
catch {
  console.log("already replaced");
  }
}
<div class="container">
  
  <h1>Header1</h1>
  <div >
    <h2>Header2</h2>
  </div>
  <p>some dummy text</p>
  
  <button>replace header2</button>
</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