Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
Hiding a button until a condition is met (Javascript)

I am making a fishing game, where I want a button to be hidden until the fish number reached 5. However, the button (set fish traps) does not seem to be hidden even the condition has been met...

let fishNum = document.getElementById("fishNum");
let traps = document.getElementById("cm2Btn2");

function addFish() {
  let fishNum = document.getElementById("fishNum");

  count.innerHTML++;
}

if (fishNum > 2) {
  traps.classList.add('fadeOut');
}
<div class="cLeftRight">
  <span id="fishNum">0</span>
</div>

<button id="cm2Btn" onclick="document.getElementById('fishNum').innerHTML++">Catch a Herring</button>

<button id="cm2Btn2">Set Fish Traps</button>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your example only checks the value of fishNum on the initial load, and fishNum is not a number but an element from the dom. Your addFish function is never used, only the inline js from the html. Your addFish function increments a variable called count, which is never defined. There is no css for the class fadeOut so the item will not fade out even if the class were added.

Please see my demo, which is a rough fix of what you were attempting to do. Some things that I would change moving forward, instead of incrementing the HTML of fishNum, I would have a variable in js that you increment, and update the screen to reflect what is happening in your js.

At the core of answering this question, I believe you are looking for the css property transition and the html attribute disabled. We can use js to enable the button when we add the fadeIn class, and make it so we transition gradually from 0 to 1 opacity.

let fishNum = document.getElementById("fishNum");
let traps = document.getElementById("cm2Btn2");

function addFish() {
  fishNum.innerHTML++;
  
  if (fishNum.innerHTML > 4) {
    traps.classList.add("fadeIn");
    traps.disabled = false;
  }
  
}
document.getElementById("cm2Btn")
  .addEventListener("click", addFish);
#cm2Btn2 {
  transition: opacity 1s;
  opacity: 0;
}

#cm2Btn2.fadeIn {
  opacity: 1;
}
<div class="cLeftRight">
  <span id="fishNum">0</span>
</div>

<button id="cm2Btn">Catch a Herring</button>

<button disabled="true" id="cm2Btn2">Set Fish Traps</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!