Tell us what’s happening:
I am trying to do a querySelectAll to grab all button elements. When I use console.log on the querySelectorAll I see it’s grabbing the right number of elements but there’s nothing in the objects. This doesn’t seem right, but I’m not sure where things are going wrong. My plan was then to do a .forEach and assign my handleClick function to update the values.
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>
<ul class="item-list">
<li>Candy <button class="favorite-icon">♡</button></li>
<li>Hottie <button class="favorite-icon">♡</button></li>
<li>Milk Shake <button class="favorite-icon">♡</button></li>
</ul>
</body>
<script src="script.js"></script>
</html>
/* file: styles.css */
.favorite-icon{
background-color: #00000000;
border: none;
font-size: 1rem;
}
.filled{
font-size: 1.5rem;
}
/* file: script.js */
//First we need to get the elements
const faveIcon = document.querySelectorAll(".favorite-icon");
console.log(faveIcon);
//then we need to have a function that toggles the filled and updated the text of the button
const handleClick = (button) =>{
if(button.innerHTML === "♡"){
button.innerHTML = "❤";
button.classList.add("filled");
}
else{
button.innerHTML = "♡";
button.classList.remove("filled");
}
};
faveIcon.forEach((button) =>
button.addEventListener("click", () =>handleClick(button))
);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Challenge Information:
Build a Favorite Icon Toggler - Build a Favorite Icon Toggler