Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

154
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda