i want to change color to green when my counter number were grater than zero and change to red when counter less than zero
const addBtn = document.getElementById(`add`);
const resetBtn = document.getElementById(`reset`);
const minusBtn = document.getElementById(`minus`);
const counter = document.getElementById(`counter`);
if (counter.innerHTML > 0) {
counter.classList.remove(`white`)
counter.classList.add(`green`)
}
addBtn.addEventListener(`click`, () => {
counter.innerHTML = Number(counter.innerHTML) + 1;
});
minusBtn.addEventListener(`click`, () => {
counter.innerHTML -= 1;
});
resetBtn.addEventListener(`click`, () => {
counter.innerHTML = 0;
});
what should i do?
5
Answers
changed a little bit but should get you closer to what you are looking for:
Create a function that checks for the value and changes the color whenever you click on
addBtn
,minusBtn
, andresetBtn
.You can define a separate function to change the count, and call it in each event handler. This function can then handle setting the color.
Create a function and update as per your requirement
Each time you click a button you have to check if the value is >/< 0 so you need to use that
If statement
in all youreventListeners
and it’s better to create a function that can do that so that you keep your code clean.