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

140
Visualizações
Best way to move loaded element

I wanna move the element which is already loaded to a different position.

I have referred to https://stackoverflow.com/a/16127049/19743544

The appendChild methods adds an element to the DOM.

insertAdjacentHTML method takes a string instead of an element, so they have to parse the string and create elements from it

I can see that both appendChildand insertAdjacentHTML are actually meant to create an element instead of moving the loaded element to somewehre.

In the example below, how do I move the div .apple into the section main before the div orange ?

const main = document.querySelector('.main'),
  apple = document.querySelector('.apple'),
  orange = document.querySelector('.orange');
<section class="main">
  <div class="orange"></div>
</section>

<div class="apple"></div>

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Use insertBefore

const main = document.querySelector('.main'),
const apple = document.querySelector('.apple'),
const orange = document.querySelector('.orange');

main.insertBefore(apple, orange);

Note that both insertBefore and appendChild (in case you want to add it in last position) will move the element if it already exists in the DOM

If the given child is a reference to an existing node in the document, appendChild() moves it from its current position to the new position.

about 4 years ago · Santiago Trujillo 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