Skip to content

Instantly share code, notes, and snippets.

@jswhisperer
Created June 9, 2013 14:16
Show Gist options
  • Select an option

  • Save jswhisperer/5743704 to your computer and use it in GitHub Desktop.

Select an option

Save jswhisperer/5743704 to your computer and use it in GitHub Desktop.
JS native equivilant to DOM Manipulation with jQuery
/* Append HTML elements 8/
/* jQuery */
$(document.body).append("<div id='myDiv'><img src='im.gif'/></div>");
/* CRAPPY native equivalent */
document.body.innerHTML += "<div id='myDiv'><img src='im.gif'/></div>";
/* MUCH BETTER native equivalent */
var frag = document.createDocumentFragment();
var myDiv = document.createElement("div");
myDiv.id = "myDiv";
var im = document.createElement("img");
im.src = "im.gif";
myDiv.appendChild(im);
frag.appendChild(myDiv);
document.body.appendChild(frag);
/* Prepend HTML elements */
/* same as above except for last line */
document.body.insertBefore(frag, document.body.firstChild);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment