aniaska4
aniaska4

Reputation: 15

how to set words from the sentence one below the other js

Hi I have problem with my words in sentence. I have sentence, I do a loop to get words, and then I want to put them in <li> - one below the other. So i did again a loop to get only words, and put them to li, but in my page I have only one word. Why its happened like that?

const wordText = document.createElement("div");
const lettersSection = document.querySelector(".letter");
        wordText.classList.add("wordText");
        lettersSection.appendChild(wordText);
        const jokeText = document.querySelectorAll(".random_joke h3");
        for (let el of jokeText) {
            const matchWords = el.textContent.match(/[a-z]+/gi);
            const changeSequenceWords = matchWords.sort(byLength);
            // const newString = changeSequenceWords.join(" ");
            for (let el of changeSequenceWords ) {
                const word = el;
                console.log(el)
                wordText.innerHTML = `<h4>Words:</h4><span><li>${word}</li></span>`
            }
            // const newString = changeSequenceWords.join(" ");

            // wordText.innerHTML = `<h4>Words:</h4><span>${newString} </span>`
        }

Upvotes: 0

Views: 89

Answers (1)

Jack Bashford
Jack Bashford

Reputation: 44145

You're removing the old value of wordText.innerHTML each time the loop runs - use +=:

wordText.innerHTML += `<h4>Words:</h4><span><li>${word}</li></span>`

Upvotes: 1

Related Questions