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

141
Vistas
Jquery Show or hide content block on click on checkbox

I have several checkboxes when clicking on a certain checkbox, I want to show some content (in my case, this is a simple block for an example) when clicking on another checkbox, the previous block should hide and display a new block, I think to solve this problem you need to apply forEach checkboxes work for me, but I cannot display blocks

  <div class="toggle-two">
    <div><small>content 1</small></div>
    <label class="switch-label">
      <input class="switch" id="switch-novice" value="switch-novice"  type="checkbox"/>
       <span class="slider round"></span>
    </label>
  </div>
  
  <div class="toggle-three">
    <div><small>content 2</small></div>
    <label class="switch-label">
      <input class="switch" id="switch-intermediate" value="switch-intermediate"  type="checkbox"/>
      <span class="slider round"></span>
    </label>
  </div>
  
  <div class="toggle-four">
    <div><small>content 3</small></div>
    <label class="switch-label">
      <input class="switch" id="switch-expert" value="switch-expert" type="checkbox" />
      <span class="slider round"></span>
    </label>
  </div>

  <!-- ------------------------------------------- -->
  
  </div>
  <div>
    <div class="content c1"></div>
    <div class="content c2"></div>
    <div class="content c3"></div>
  </div>

**script.js**

let uploadPres = document.getElementsByClassName("content");

$(".content").each(function (index, value) {
  $(`.switch:not([checked])`).on("change", function (param1, param2) {
    $(".switch").not(this).prop("checked", false);

    if ($(".switch").is(":checked")) {
      console.log(this.checked);
    }
  });
});

Initially a class named content has display: none by default

You can also see this example in codesandbox

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

on each input checkbox add the reference of the associated content div like below.

<input class="switch" id="switch-novice" value="switch-novice"  type="checkbox" data-content="c1"/>
 OR
<input class="switch" id="switch-novice" value="switch-novice"  type="checkbox" data-content="c2"/>

Now simply update your code like below.

$(`.switch`).on("change", function (a, b) {
  $(".switch").not(this).prop("checked", false);

  if ($(".switch").is(":checked")) {
    var contentToOpen = $(this).data("content"); // read the associated class of the div

    $(`.content`).hide(); // hide all existing opened ones
    $(`.${contentToOpen}`).show(); // show the associated one
  }
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

function toggle(event){
  var container = event.target.closest(".togglers");
  var currentContainer = event.target.closest(".toggle");
  var index = Array.from(container.children).indexOf(currentContainer);
  
  var checkboxes = document.querySelectorAll(".toggle input");
  $(checkboxes).prop("checked",false);
  $(event.target).prop("checked", true);

  var contentItems = document.querySelectorAll(".content-items .content");
  $(contentItems).hide();
  var content = contentItems[index];
  $(content).slideToggle();
}
.content {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="togglers">
  <div class="toggle"><input type="checkbox" onclick="toggle(event)" />
    toggle 1
   </div>
  <div class="toggle"><input type="checkbox" onclick="toggle(event)" />
    toggle 2
   </div>
  <div class="toggle"><input type="checkbox" onclick="toggle(event)" />
    toggle 3
   </div>
</div>

<div class="content-items">
  <div class="content">Content 1</div>
  <div class="content">Content 2</div>
  <div class="content">Content 3</div>
</div>

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