Created
January 8, 2018 09:07
-
-
Save ivan-stepaniuk/3f5b979a34cbe94f53e280a1c1cee9b4 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