Created
January 8, 2018 09:07
-
-
Save ivan-stepaniuk/8595b5a36e4c581203dac86cd7cc49aa to your computer and use it in GitHub Desktop.
Wrapping an element in Vanilla JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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