Skip to content

Instantly share code, notes, and snippets.

@sdoro
Last active January 14, 2018 15:13
Show Gist options
  • Save sdoro/c878ea0111a3fc8742f0785422a0b588 to your computer and use it in GitHub Desktop.
Save sdoro/c878ea0111a3fc8742f0785422a0b588 to your computer and use it in GitHub Desktop.
Moving Elements
<ul id="yours">
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
</ul>
<ul id="mine">
<li>First</li>
<li>Second</li>
<li>Third</li>
</ul>

Moving Elements

A Pen by Bre'Ana Deen on CodePen.

License.

Modify the example above so that it moves the fourth element from the first list, and places it as the first list item in the second list.

var list1 = document.getElementById("yours");
var list2 = document.getElementById("mine");
var item = list1.children[3]; // obtain the fourth element from the first list
listItems = list2.children;
list2.insertBefore(item, listItems[0]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment