Home > Mobile >  Jquery Show or hide content block on click on checkbox
Jquery Show or hide content block on click on checkbox

Time:11-20

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

CodePudding user response:

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
  }
});

CodePudding user response:

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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related