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

134
Visualizações
Create a new element div and wrap it around an existing one with JavaScript

I am aware how to create a new div or any other HTML element and add it as a child using pure JavaScript.

Am I able to create a new element and add my existing one into it?

For example:

I would like to take the current code below and put it within a brand new div.

// Current elements on page
<div>
  <img />
  <img />
</div>
<div>
  <p>Hello world</p>
</div>

What I want:

// What I want
<div class="my-new-div">
 <div>
  <img />
  <img />
 </div>
 <div>
  <p>Hello world</p>
 </div>
</div>

I understand this is easy to do in HTML however. I have to use JavaScript in this scenario to alter a webpage. In this scenario I must add a parent div to avoid changing styles.

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

0

You can simply fetch the existing node, create a new one, insert it before the existing node, and finally append the existing node to the new node as child. The browser moves the node when performing the "append" operation, if it is already contained somewhere in the document.

const myDiv = document.getElementById("myDiv");
const myNewDiv = document.createElement("div");
myNewDiv.style.border = "solid 1px";

document.body.insertBefore(myNewDiv, myDiv);
myNewDiv.appendChild(myDiv);
<div id="myDiv">
  <p>Hello</p>
  <p>Paragraph</p>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

To move one element from one part of the document to another part, you need to associate the element to a new parent. this automatically disassociates the moving element from its old parent.

function newAndMove() {

  //Create a new element
  let blue = document.createElement('DIV');
  blue.classList.add('blue-div');
  document.body.appendChild(blue);
  
  //Move existing element inside it
  let red = document.getElementById('old-div');  
  blue.appendChild(red);  
  
}
.red-div {
  width: 200px;
  height: 200px;
  background-color: red; 
}

.blue-div {
  width: 400px;
  height: 400px;
  background-color: blue;
  position: absolute;
  top: 100px;
  left: 100pxl
}
<input type="button" value="Get red inside new blue" onClick="newAndMove()">
<div class="red-div" id="old-div"></div>

you can move DOM elements by appending them as a child to the preferred container.

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