Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<div>
  <div>
    <label id="radio" for="select_radio">Select x</label>
  </div>
  <div>
    <div>
      <input name="select_radio" value="Yes" id="radio_yes" type="radio" checked>
      <label id="label_radio_yes" for="yes">Yes </label>
    </div>
    <div>
      <input name="select_radio" value="No" id="radio_no" type="radio">
      <label id="label_radio_no" for="no">No </label>
    </div>
  </div>
</div>
<div class="sub-element">
  <div class="grandparent">
  <p>I am the grandparent 1</p>
  <div class="parent">
    <p>I am the parent</p>
    <div class="child">
      <p>I am the child</p>
      <input class="child" value="Y" id="checkbox_0" type="checkbox">
      <label id="label_2" for="checkbox_2" class="child">Add another</label>
    </div>
  </div>
</div>
<div class="grandparent">
  <p>I am the grandparent 2</p>
  <div class="parent">
    <p>I am the parent</p>
    <div class="child">
      <p>I am the child</p>
      <input class="child" value="Y" id="checkbox_1" type="checkbox">
      <label id="label_3" for="checkbox_3" class="child">Add another </label>
    </div>
  </div>
</div>
<div class="grandparent">
  <p>I am the grandparent 3</p>
  <div class="parent">
    <p>I am the parent</p>
    <div class="child">
      <p>I am the child</p>
    </div>
  </div>
</div>
<script src="https://code.jquery.com/jquery-2.0.3.js"></script>
</div>
</body>
</html>
 
var grandParents = $(".grandparent");
var subContainer = $(".sub-element");
var checkbox_0 = $("#checkbox_0");
var checkbox_1 = $("#checkbox_1");
var state = {
  open: false,
  checkbox_0: false,
  checkbox_1: false
};
//hide everything on load at first except for first element
grandParents.each( function(index, el){
  if(index === !0){
      $(this).hide();
  }
});
//check the radio value on pageload
checkRadioOnLoad();
//check inputs on pageload
checkInputs();
         
$(":radio[name='select_radio']").on('change', function() {
    
  var masterSelect = $(this).val();
  
  if(masterSelect === "Yes"){
    state.open = true;
  }else{
    state.open = false;
  }
  
  toggleContainer();
  
});
$(checkbox_0).on('change', function() {
  if(this.checked){
    state.checkbox_0 = true;
  }else{
    state.checkbox_0 = false;
  }
  
  //re-run check
  checkInputs();
  
});
$(checkbox_1).on('change', function() {
  if(this.checked){
    state.checkbox_1 = true;
  }else{
    state.checkbox_1 = false;
  }
  
  //re-run check
  checkInputs();
  
});
function toggleContainer(){
  if(state.open === true){
    subContainer.show();
  }else{
    subContainer.hide();
  }
}
function checkInputs(){
  if(state.checkbox_0){
    checkbox_1.parent().parent().parent().show();
  }else{
    checkbox_1.parent().parent().parent().hide();
  }
  
  if(state.checkbox_1){
    $(grandParents[2]).show();
  }else{
    $(grandParents[2]).hide();
  }
}
function checkRadioOnLoad(){
  if($("#radio_yes")){
    state.open = true;
  }else{
    state.open = false;
  }
}
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers