Skip to content

Instantly share code, notes, and snippets.

@stpettersens
Created January 24, 2016 15:49
Show Gist options
  • Save stpettersens/95637eea007378baa889 to your computer and use it in GitHub Desktop.
Save stpettersens/95637eea007378baa889 to your computer and use it in GitHub Desktop.
Wrap function (@mdjasper).
/*
Michael Jasper's (@mdjasper) wrap DOM element function.
From: http://www.mikedoesweb.com/2014/wrapping-an-element-in-vanilla-javascript/
*/
function wrap(top, selector, bottom){
var matches = document.querySelectorAll(selector);
for (var i = 0; i < matches.length; i++){
var modified = top + matches[i].outerHTML + bottom;
matches[i].outerHTML = modified;
}
}
// Example usage: wrap("<div class='wrapper'>", ".getWrapped", "</div>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment