Skip to content

Instantly share code, notes, and snippets.

@randyjensen
Created July 27, 2013 23:53
Show Gist options
  • Save randyjensen/6096774 to your computer and use it in GitHub Desktop.
Save randyjensen/6096774 to your computer and use it in GitHub Desktop.
Native JS for jQuery Functions
//----Append HTML elements----
/* jQuery */
$(document.body).append("<div id='myDiv'><img src='im.gif'/></div>");
/* 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);
//----Adding a class------
/* native equivalent */
el.classList.add("someClass");
//----Removing a class-----
/* native equivalent */
el.classList.remove("someClass");
//----Does it have class---
/* native equivalent */
if(el.classList.contains("someClass"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment