Here is some code that appears to get your required functionality:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hearts</title>
</head>
<body>
<img id="foodHeart1" src="images/heartImage.png" alt="emptyHeart" height="50px">
<img id="playHeart1" src="images/heartImage.png" alt="emptyHeart" height="50px">
<button onclick="changeImage()">Change to empty Heart</button>
<button id="reset">Reset</button>
</body>
<script src="index.js" defer></script>
</html>
const resetButton = document.getElementById("reset");
const foodHeart1 = document.getElementById("foodHeart1");
const playHeart1 = document.getElementById("playHeart1");
function reset() {
location.reload();
}
resetButton.addEventListener("click", reset);
// Hide reset button until no hearts left
function hideReset() {
const foodHeartMatch = foodHeart1.src.match("images/emptyheart.png");
const foodHeartEmpty = foodHeartMatch && foodHeartMatch.length === 1;
const playHeartMatch = playHeart1.src.match("images/emptyheart.png");
const playHeartEmpty = playHeartMatch && playHeartMatch.length === 1;
console.log(`${foodHeartEmpty} : ${playHeartEmpty}`);
if (foodHeartEmpty && playHeartEmpty) {
resetButton.style.display = "inline";
} else {
resetButton.style.display = "none";
}
}
hideReset();
// Change the images to empytyHear
function changeImage() {
foodHeart1.src = "images/emptyheart.png";
playHeart1.src = "images/emptyheart.png";
hideReset();
}
I don't believe there is a style.display = "visible", but I hope someone will correct me if that is wrong. (From a quick look at MDN. I have set it to inline here but you can use any appropriate value to display the button.
I also used the length operator on the match function as it returns an array (for your conditional statemnt).
As James mentioned in the comment the hideReset() function is likely going to be called where you update your heart values. In this case I have put it in the function that changes the images.
There will be many ways to improve this but I hope it gets you over the initial bump you're facing
hideReset()
being invoked? When it's invoked, what is the result of theif
condition(s)? Does it match the expected result? If not, what was the expected result and why? Can you update this to a runnable minimal reproducible example which demonstrates the problem?hideReset()
from within that function too.visible
value for thedisplay
property.display
doesn't have 2 values.inline
,inline-block
,flex
,inline-flex
,table
these are also values ofdisplay
property and there are more.visibility
property. It has 2 values:visible
andhidden
.