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

153
Views
If both checkboxes are true, then show DIV

I am trying to show a Div that contains a button element, but only when BOTH checkboxes have been "agreed" to. I have tried to use JS to check this and then set the style display when each checkbox is clicked.

HTML:

<div class="agreement_box">
  <input type="checkbox" id="box1" onclick="showMe('submit_btn')">
</div>
<br>
<div class="agreement_box">
  <input type="checkbox" id="box2" onclick="showMe('submit_btn')">
</div>

<div id="submit_btn" class="profileSubmit_btn" style="display:none">
  <button>
  BUTTON
  </button>
</div>

JS:

function showMe(box) {
  var chbox1 = document.getElementByID("box1");
  var chbox2 = document.getElementByID("box2");
  var vis = "none";

  if (chbox1.checked && chbox2.checked) {
    vis = "block";
    break;
  }

  document.getElementById(box).style.display = vis;
}

Fiddle: https://jsfiddle.net/nhykqodp/2/

I'm kinda new to JS and my HTML knowledge is about 10 years out of date at this point. Any help would be appreciated.

Thanks.

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

0

If I run your code, it shows:

Uncaught SyntaxError: Illegal break statement

Thats because a break can only be used inside a loop.


Furthermore, you have a typo, it should be getElementById instead of getElementByID

Note: The d shouldn't be capitalised


  • Remove the break
  • Fix the typos:

function showMe(box) {
  var chbox1 = document.getElementById("box1");
  var chbox2 = document.getElementById("box2");
  var vis = "none";

  if (chbox1.checked && chbox2.checked) {
    vis = "block";
  }

  document.getElementById(box).style.display = vis;
}
<div class="agreement_box">
  <input type="checkbox" id="box1" onclick="showMe('submit_btn')">
</div>
<br>
<div class="agreement_box">
  <input type="checkbox" id="box2" onclick="showMe('submit_btn')">
</div>

<div id="submit_btn" class="profileSubmit_btn" style="display:none">
  <button>
  BUTTON
  </button>
</div>


Using an in-line if statement, we can remove the vis variable so we can alter the style right away like so:

function showMe(box) {
  const chbox1 = document.getElementById("box1");
  const chbox2 = document.getElementById("box2");
  document.getElementById(box).style.display = (chbox1.checked && chbox2.checked) ? 'block' : 'none';
}

function showMe(box) {
  const chbox1 = document.getElementById("box1");
  const chbox2 = document.getElementById("box2");
  document.getElementById(box).style.display = (chbox1.checked && chbox2.checked) ? 'block' : 'none';
}
<div class="agreement_box">
  <input type="checkbox" id="box1" onclick="showMe('submit_btn')">
</div>
<br>
<div class="agreement_box">
  <input type="checkbox" id="box2" onclick="showMe('submit_btn')">
</div>

<div id="submit_btn" class="profileSubmit_btn" style="display:none">
  <button>
  BUTTON
  </button>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

The syntax errors have already been pointed out; for hiding/showing elements these already have an API which is called el.hidden. I recommend you use it.

Aside from that, you shouldn't use inline event handlers like onclick. Instead, use EventTarget.addEventListener().

I've also generalized your code, so that any number of checkboxes can act as toggle for any element, given that element has the id that you define in data-toggle on the checkboxes.

const checkboxes = Array.from(document.querySelectorAll('[data-toggle]'));

function allChecked(toggle) {
  return checkboxes.filter(cb => cb.dataset.toggle === toggle).every(cb => cb.checked);
}

for (const checkbox of checkboxes) {
  checkbox.addEventListener('change', function() {
    document.getElementById(this.dataset.toggle).hidden = !allChecked(this.dataset.toggle);
  });
}
<div class="agreement_box">
  <input type="checkbox" data-toggle="submit_btn">
</div>
<br>
<div class="agreement_box">
  <input type="checkbox" data-toggle="submit_btn">
</div>

<div id="submit_btn" class="profileSubmit_btn" hidden>
  <button>
    BUTTON
  </button>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

Little syntax error just replace document.getElementByID to document.getElementById

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!