Skip to content

Instantly share code, notes, and snippets.

@AllThingsSmitty
Last active August 29, 2015 14:19
Show Gist options
  • Save AllThingsSmitty/d4d2ea1f1a15f878f642 to your computer and use it in GitHub Desktop.
Save AllThingsSmitty/d4d2ea1f1a15f878f642 to your computer and use it in GitHub Desktop.
Quick tip about using appendChild() and insertBefore()
// When using appendChild() or insertBefore() on an element that's already been added to the document, the element will be removed from its place and put into the new place
var b1 = document.querySelector('.box1'),
b2 = document.querySelector('.box2'),
el = document.createElement('div'),
t;
b1.appendChild(el);
t = window.setTimeout(function () {
b2.appendChild(el);
clearTimeout(t);
}, 3000); // setTimeout() with a 3s delay
// We can easily see the div inserted into box1, then after the specified delay inserted again into box2
// This way we can see that even though the div is inserted twice, it's still one element
.box1 div,
.box2 div {
height: 100px;
width: 100px;
}
/* Background colors visually differentiate between a div inserted into box1 compared to a div inserted into box2 */
.box1 div {
background: #c00;
}
.box2 div {
background: #ffa500;
}
<div class="box1"></div>
<div class="box2"></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment