Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

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

Select an option

Save ivan-stepaniuk/8595b5a36e4c581203dac86cd7cc49aa 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