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

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

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

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 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