kreativan
4/4/2018 - 8:44 PM

Get First Word JS

Wrap frist word of a string in span. JavaScript

/**
 *  Wrap first work of a string into span
 *
 */
function firstWord() {
    var firstWordArr = document.querySelectorAll(".tm-word-color");
    firstWordArr.forEach(e => {
        var arr = e.innerHTML.split(" ");

        var newHTML = "<span>"+arr[0]+"</span>"
        for(i = 1; i < arr.length; i++) {
            newHTML += " " + arr[i];
        }

        e.innerHTML = newHTML;

    });
}

firstWord();