Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save ivan-stepaniuk/3f5b979a34cbe94f53e280a1c1cee9b4 to your computer and use it in GitHub Desktop.

Select an option

Save ivan-stepaniuk/3f5b979a34cbe94f53e280a1c1cee9b4 to your computer and use it in GitHub Desktop.
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;
}
}
wrap("<div class='wrapper'>", ".getWrapped", "</div>");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment