+ 1

How to swap html elements with JavaScript?

For example we have 2 div elements with id's "div1" and "div2". In the original html div2 goes after div1, but how to swap them with js?

24th Mar 2018, 2:39 PM
Evil Dog
Evil Dog - avatar
1 Answer
+ 4
You remove div1 and create it again. var div = document.getElementById("div1"); div.remove(); div = document.createElement("div"); div.setAttribute("id", "div1"); document.getElementById("parent").appendChild(div);
24th Mar 2018, 3:18 PM
Toni Isotalo
Toni Isotalo - avatar