Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Indeterminate Checkboxes</h1>

<ul>
  <li>
    <input type="checkbox" name="tall" id="tall">
    <label for="tall">Tall Things</label>

    <ul>
      <li>
        <input type="checkbox" name="tall-1" id="tall-1">
        <label for="tall-1">Buildings</label>
      </li>
      <li>
        <input type="checkbox" name="tall-2" id="tall-2">
        <label for="tall-2">Giants</label>

        <ul>
          <li>
            <input type="checkbox" name="tall-2-1" id="tall-2-1">
            <label for="tall-2-1">Andre</label>
          </li>
          <li>
            <input type="checkbox" name="tall-2-2" id="tall-2-2">
            <label for="tall-2-2">Paul Bunyan</label>
          </li>
        </ul>
      </li>
      <li>
        <input type="checkbox" name="tall-3" id="tall-3">
        <label for="tall-3">Two sandwiches</label>
      </li>
    </ul>
  </li>
  <li>
    <input type="checkbox" name="short" id="short">
    <label for="short">Short Things</label>

    <ul>
      <li>
        <input type="checkbox" name="short-1" id="short-1">
        <label for="short-1">Smurfs</label>
      </li>
      <li>
        <input type="checkbox" name="short-2" id="short-2">
        <label for="short-2">Mushrooms</label>
      </li>
      <li>
        <input type="checkbox" name="short-3" id="short-3">
        <label for="short-3">One Sandwich</label>
      </li>
    </ul>
  </li>
</ul>

<p>This version is a fork of <a href="https://codepen.io/chriscoyier/pen/JYyXjX">https://codepen.io/chriscoyier/pen/JYyXjX</a> posted on twitter at <a href="https://twitter.com/chriscoyier/status/1167014134191468546">https://twitter.com/chriscoyier/status/1167014134191468546</a></p>
              
            
!

CSS

              
                body {
  padding: 20px;
}
ul { 
  list-style: none;
  margin: 5px 20px;
}
li {
  margin: 10px 0;
}
              
            
!

JS

              
                var inputSelector = 'input[type="checkbox"]';
var listSelector = 'ul';
// select the root of the complete list
var mainListElement = document.querySelector(listSelector);
// select ALL checkboxes that are in the list
let checkboxes = mainListElement.querySelectorAll(
  'ul input[type="checkbox"]'
);

// apply the event listener to all items
checkboxes.forEach((element) => {
  element.addEventListener('click', () => {
    switchChilds.call(element);
    if (this.checked) {
      this.indeterminate = false;
    }
    indeterminateParents.call(element, mainListElement);
  });
});

// selects all child input checkboxes and applies the checked
// item of the one that has been clicked on
function switchChilds() {
  let childs = this.parentElement.querySelectorAll(listSelector + ' ' + inputSelector);
  childs.forEach((element) => {
    element.checked = this.checked;
  });
}

// checks if the childs is somewhere in the DOM of the parent
function contains(parent, child) {
  /*
  found at:
  https://stackoverflow.com/questions/2234979/how-to-check-in-javascript-if-one-element-is-contained-within-another
  */
  var node = child.parentNode;
     while (node != null) {
         if (node == parent) {
             return true;
         }
         node = node.parentNode;
     }
     return false;
}

// checks if all given checkboxes are unchecked
function allUnchecked(checkboxes) {
  var isChecked = false;
  checkboxes.forEach((item, index) => {
    if (item.checked) {
      isChecked = true;
    }
  });
  return !isChecked;
}

// updates the indeterminate status of the parents
function indeterminateParents(ul) {
  let childs = ul.childNodes;
  for (i = 0; i < childs.length; ++i) {
    if (contains(childs[i], this) === false) {
      continue;
    }
    let nextCheckbox = childs[i].querySelector(inputSelector);
    if (nextCheckbox === this) {
      return true;
    }
    let allDescendantBoxes = childs[i].querySelectorAll(inputSelector);
    if (allUnchecked(allDescendantBoxes)) {
      nextCheckbox.indeterminate = false;
    } else {
      nextCheckbox.indeterminate = true;
    }
    indeterminateParents.call(this, childs[i].querySelector(listSelector));
    break;
  }
  return true;
}

              
            
!
999px

Console