lundi 23 janvier 2017

uncheck a group of checked input type="checkbox" with a single button

i have a #main-container with groups of input type checkboxes. these checkboxes open up button lists when checked.

the #main-container that holds all of this can be closed by a button. the problem i have is how do i make this button that closes #main-container also uncheck any checkboxes that are left checked when the #main-container is closed?

at the bottom i have included the only way i know to do it with javascript at the moment but that means making a variable for each checkbox and that checkbox must have a unique ID. i am trying to avoid that.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>input checkbox</title>

<style>


body {

margin: 0;
background: beige;

border: 1px solid blue;
}


.main-container {
border: 1px solid red;
width: 25%;

position: relative;

}

.toggle-box {
  display: none;

}

label {
  margin: 0.2em;
  display: block;
  text-align: center;
  transition: all 1s linear;
  border: 1px solid green;

}

.toggle-box + div {
  display: none;
  margin: 0.2em;
  text-align: center;

}

.toggle-box:checked + div {
  display: block;
  border: 1px solid black;
  transition: all 1s linear;

}

button {
   display: block;
   margin: auto;
   width: 100%;

}


</style>  
</head>

<body>
  <div id="main-container" class="main-container">
    <label for="toggle-box">A</label>
    <input class="toggle-box" id="toggle-box" type="checkbox" >
    <div>
      <button type="button">submit</button>
      <button type="button">submit</button>
    </div>
    <label for="toggle-box2">B</label>
    <input class="toggle-box" id="toggle-box2" type="checkbox" >
    <div>
      <button type="button">submit</button>
      <button type="button">submit</button> 
    </div>
    <div></div>
    <div></div>
    <div class="test1"></div>


  </div>

  <button type="button" onclick="closeMe()">close</button>
  <button type="button" onclick="openMe()">open</button>




<script>

function closeMe() {
var $toggleBox = document.getElementById("toggle-box");
$toggleBox.checked = false;
document.getElementById("main-container").style.display = "none";

}

function openMe() {
document.getElementById("main-container").style.display = "block";

}


</script> 

</body>
</html>




Aucun commentaire:

Enregistrer un commentaire