Last active
February 1, 2022 01:02
-
-
Save gosseti/1629106ad8dc7e3823f31543ff1f4552 to your computer and use it in GitHub Desktop.
makeSpans.js
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
'use strict' | |
function _toArray(arr) { return Array.isArray(arr) ? arr : Array.from(arr) } | |
function makeSpans (selector) { | |
var _document$querySelect = document.querySelectorAll(selector) | |
var _document$querySelect2 = _toArray(_document$querySelect) | |
var elements = _document$querySelect2.slice(0) | |
return elements.map(function (element) { | |
var text = element.innerText.split('') | |
var spans = text.map(function (letter) { | |
return '<span>' + letter + '</span>' | |
}).join('') | |
return element.innerHTML = spans | |
}) | |
} | |
// usage with single selector | |
makeSpans('h1') | |
// or with multiple selectors | |
makeSpans('h1, h2, .heading') |
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
// this function takes text and wraps all the letters in spans | |
const makeSpans = selector => { | |
const [...elements] = document.querySelectorAll(selector) | |
return elements.map(element => { | |
const text = element.innerText.split('') | |
const spans = text | |
.map(letter => '<span>' + letter + '</span>') | |
.join('') | |
return element.innerHTML = spans | |
}) | |
} | |
// usage with single selector | |
makeSpans('h1') | |
// or with multiple selectors | |
makeSpans('h1, h2, .heading') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment