I'm trying to highlight all the words that have more than 5 characters within a <p>
element. I was able to select those words, but I failed on highlighting those words on the screen. I'd appreciate any help. Please see the complete code JavaScript exercise
let pElement = document.getElementById("text");
function highlightLongWords(element){
let paragraph = pElement.textContent;
let textInsideP = pElement.innerHTML = paragraph;
// use String.split(" ") to split the paragraph into an array of words
let words = paragraph.split(" ");
console.log(words);
let longestWord = "abcde";
//use Array.length to get the length of the array.
for(let word of words) {
if (word.length > longestWord.length) {
//console.log(word);
let longWords = word;
pElement.style.backgroundColor = "yellow"
console.log(longWords);
}
}
return;
}
highlightLongWords(pElement);