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

253
Views
what is the fast and the easiest way to change the order of divs in html with javascript

I want to move div1 and div2 to the end of list

with only vanilla javascript

<div class="container">
   <div id="chlid-1"></div>
   <div id="chlid-2"></div>
   <div id="chlid-3"></div>
   <div id="chlid-4"></div>
   <div id="chlid-5"></div>
</div>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use CSS flex on a parent, and the order property on the desired child elements

.container {
  display: flex;
  flex-direction: column;
}

#child-1,
#child-2 {
  order: 1;
}
<div class="container">
   <div id="child-1">1</div>
   <div id="child-2">2</div>
   <div id="child-3">3</div>
   <div id="child-4">4</div>
   <div id="child-5">5</div>
</div>

Using JavaScript - use the Element.append() method like:

// DOM utils:

const ELS = (sel, par) => (par || document).querySelectorAll(sel);
const EL = (sel, par) => (par || document).querySelector(sel);


// Task:

EL(".container").append(...ELS("#child-1, #child-2"));
<div class="container">
   <div id="child-1">1</div>
   <div id="child-2">2</div>
   <div id="child-3">3</div>
   <div id="child-4">4</div>
   <div id="child-5">5</div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

You can do the following and the old div will be removed automatically

var element = document.getElementById('child1');
var parent = element.parentNode;
parent.insertAfter(element, parent.lastChild);
about 4 years ago · Juan Pablo Isaza 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!