I want to make sure that when the span element turns lime, an alert box shows up, however, I tried using both hasAttribute() and getAttribute() to try to make the if statement function properly, but both did not work for me, the error may be occurring because the innerHTML is because configured in a forEach() function.
So far, when I click enter with the word "Absolute" put in the div boxes, the div boxes turn green, but no alert shows up.
Please help! Thank you in advance. Link to all lines of code just in case, sorry it may be a bit messy, I have a lot of code for a sololearn: https://code.sololearn.com/WX59M6HHngc5
const pText = document.querySelector("#p123").innerText.toUpperCase()
const button = document.querySelector("#ENTER")
Array.from(document.querySelectorAll(".amot")).forEach(function(element) {
button.addEventListener("click", () => {
const text = element.textContent.toUpperCase()
if (text.match(/\d/gi) || text.length < 1 || text.match(/[^a-z]/gi)) {
element.innerText = text
return
}
element.innerHTML = ""
text.split("").forEach((letter, i) => {
if (pText.includes(letter) && pText[i] === letter) {
element.innerHTML += `<span id = "span1" style="color: lime">${letter}</span>`
} else if (pText.includes(letter)){
element.innerHTML += `<span style="color: orange">${letter}</span>`
} else {
element.innerHTML += `<span style="color: lightgrey">${letter}</span>`
}
})
})
let sp1 = document.getElementById("span1");
if(sp1.hasAttribute('style') == "color: lime") {
alert("Test");
};
From the above comment ...
The
CustomEvent
approach for code one does own and is allowed to change, and where decoupling makes sense ...The
MutationObserver
approach for code one does not own and thus can not change/adapt to ones needs ...