123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>
            
          
!
            
              body {
  padding: 20px;
}
ul { 
  list-style: none;
  margin: 5px 20px;
}
li {
  margin: 10px 0;
}
            
          
!
            
              // 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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console