Tell us what’s happening:
My “.filled” class is toggling on and off like it should, but once the heart icon changes it doesn’t change back when clicked. Any help would be great, thanks!
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Favorite Icon Toggler</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<h1>Best Kinds of Cats:</h1>
<ul class="item-list">
<li>Ginger Cats <button class="favorite-icon">♡</button></li>
<li>Tuxedo Cats <button class="favorite-icon">♡</button></li>
<li>Tabby Cats <button class="favorite-icon">♡</button></li>
</ul>
<script src="./script.js"></script>
</body>
</html>
/* file: styles.css */
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
display: flex;
align-items: center;
justify-items: center;
flex-direction: column;
font-family: sans-serif;
}
h1 {
margin: 20px;
color: teal;
}
ul {
list-style-type: none;
border: 5px double teal;
margin: 20px;
padding: 30px;
border-radius: 40px;
line-height: 2;
}
button {
padding: 3px 6px;
border-radius: 20px;
}
.filled {
border: 2px solid teal;
}
/* file: script.js */
// Node list of buttons
const btns = document.querySelectorAll(".favorite-icon");
// toggle class .filled & set icon
const toggleIcon = (btn) => {
btn.classList.toggle("filled");
btn.innerHTML === "❤" ?
btn.innerHTML = "♡" :
btn.innerHTML = "❤";
};
// adds event listener to each btn
btns.forEach((btn) => {
btn.addEventListener("click", () => toggleIcon(btn))
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Challenge Information:
Build a Favorite Icon Toggler - Build a Favorite Icon Toggler