Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

138
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!