Spanify Text

Fonction permettant de créer un span pour chaque lettre de la phrase

<div class="hero"><span class="original-span">My Awesome Text</span></div>
let spanifyText = (sentenceDiv) => {
    let originalSpan = sentenceDiv.querySelector('.original-span');
    let text = sentenceDiv.textContent;
    let letters = text.split('');
    letters.forEach((letter) => {
        let newSpan = document.createElement('span');
        let newContent = document.createTextNode(letter);
        newSpan.appendChild(newContent);
        if (newSpan.textContent == ' ') {
            newSpan.style.marginRight = '5px';
            newSpan.style.marginLeft = '5px';
        }
        sentenceDiv.appendChild(newSpan);
    });
    originalSpan.parentNode.removeChild(originalSpan);
};

let hero = document.querySelector('.hero');
spanifyText(hero);