Many thanks. What would be the best javascript method to use to check the attribute/class of a html element? I cannot find one to use. I tried “getAttribute” but the test still fails.
function toggle1() {
if (icon[0].getAttribute("class") != "filled") {
icon[0].innerHTML = "❤"
icon[0].setAttribute("class", "filled")
}
else if (icon[0].getAttribute("class") == "filled") {
Still no luck, tried classList == "filled” and classList.value == “filled” but both fail test 8 still. Perhaps there are more errors elsewhere in my code?
function toggle1() {
if (icon[0].classList.value != "filled") {
icon[0].innerHTML = "❤"
icon[0].setAttribute("class", "filled")
}
else if (icon[0].classList.value == "filled") {
icon[0].innerHTML = "♡"
icon[0].removeAttribute("class", "filled")
}
function toggle2() {
if (icon[1].classList.value != "filled") {
icon[1].innerHTML = "❤"
icon[1].setAttribute("class", "filled")
}
else if (icon[1].classList.value == "filled") {
icon[1].innerHTML = "♡"
icon[1].removeAttribute("class", "filled")
}
}
function toggle3() {
if (icon[2].classList.value != "filled") {
icon[2].innerHTML = "❤"
icon[2].setAttribute("class", "filled")
}
else if (icon[2].classList.value == "filled") {
icon[2].innerHTML = "♡"
icon[2].removeAttribute("class", "filled")
}
}
hmm, I’m unsure what you mean, as my code now checks if the button has the class, and removes the class from the button if the class is present, as the lab directs. The app also runs as intended. Sorry I am slow, but I have read how the classList method works and still don’t understand what I am missing.
function toggle1() {
if (icon[0].classList.value != "filled") {
icon[0].innerHTML = "❤"
icon[0].setAttribute("class", "filled")
}
else if (icon[0].classList.value == "filled") {
icon[0].innerHTML = "♡"
icon[0].classList.remove("filled")
}
}
function toggle2() {
if (icon[1].classList.value != "filled") {
icon[1].innerHTML = "❤"
icon[1].setAttribute("class", "filled")
}
else if (icon[1].classList.value == "filled") {
icon[1].innerHTML = "♡"
icon[1].classList.remove("filled")
}
}
function toggle3() {
if (icon[2].classList.value != "filled") {
icon[2].innerHTML = "❤"
icon[2].setAttribute("class", "filled")
}
else if (icon[2].classList.value == "filled") {
icon[2].innerHTML = "♡"
icon[2].classList.remove("filled")
}
}
Yes, on that page under the heading “classList Properties and Methods”, you should see a table with the classList property’s methods and their description. The value() method is listed at the bottom.