Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

155
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda