Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

141
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda