Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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 (Vanilla)</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>

            <ul>
              <li>
                <input type="checkbox" name="tall-2-2-1" id="tall-2-2-1">
                <label for="tall-2-2-1">Paul</label>
              </li>
              <li>
                <input type="checkbox" name="tall-2-2-2" id="tall-2-2-2">
                <label for="tall-2-2-2">Bunyan</label>
              </li>
            </ul>
          </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><small><em>(This is the vanilla version of <a href="https://codepen.io/chriscoyier/pen/JYyXjX">https://codepen.io/chriscoyier/pen/JYyXjX</a>, sparked by <a href="https://twitter.com/chriscoyier/status/1167014134191468546">https://twitter.com/chriscoyier/status/1167014134191468546</a>)</em></small></p>
              
            
!

CSS

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

JS

              
                // Vanilla version of https://codepen.io/chriscoyier/pen/JYyXjX
const getParentCheckbox = $container => {
  const $checkboxes = Array.from(
    $container.parentElement.parentElement.children // @note: we rely heavily on the hierarchy of the markup here …
  ).filter($el => {
    return $el.nodeName === "INPUT";
  });

  return $checkboxes.length ? $checkboxes[0] : null;
};

const checkSiblings = ($container, checked) => {
  const $siblings = $container.parentElement.children;
  
  // Determine if all siblings are checked/unchecked too
  const allSiblingsHaveSameState = Array.from($siblings).reduce((result, $sibling) => {
    const $siblingCheckbox = $sibling.querySelector('input[type="checkbox"]');   
    return (
      result &&
      $siblingCheckbox.checked === checked && 
      !$siblingCheckbox.indeterminate
    );
  }, true);

  // Get Parent Checkbox
  let $parentCheckbox = getParentCheckbox($container);

  if ($parentCheckbox) {
    // All siblings checked/unchecked?
    // ~> Check/Uncheck parent too, and then check its siblings too
    if (allSiblingsHaveSameState) {
      // Set parent checkbox to same checked
      $parentCheckbox.checked = checked;
      $parentCheckbox.indeterminate = false;

      // Check parent checkbox's siblings too (if any)
      checkSiblings($parentCheckbox.parentElement, checked);
    } else {
      // Some siblings checked, some unchecked
      // ~> Set all parent checkboxes to indeterminate
      do {
        $parentCheckbox.checked = false;
        $parentCheckbox.indeterminate = true;
      } while ($parentCheckbox = getParentCheckbox($parentCheckbox.parentElement));
    }
  }
};

document.querySelectorAll('input[type="checkbox"]').forEach($checkbox => {
  $checkbox.addEventListener("change", function() {
    const checked = this.checked,
      $container = this.parentElement,
      $childCheckboxes = $container.querySelectorAll('input[type="checkbox"]');

    // Check/Uncheck all children too
    $childCheckboxes.forEach($childCheckbox => {
      $childCheckbox.indeterminate = false;
      $childCheckbox.checked = checked;
    });

    // Check siblings
    checkSiblings($container, checked);
  });
});

              
            
!
999px

Console