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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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>getCommonAncestor.js</h1>

<p>This is a demonstration of <a href="https://gist.github.com/kieranbarker/cd86310d0782b7c52ce90cd7f45bb3eb">a helper function I wrote</a> to get the closest common ancestor of two or more elements. It's useful for <a href="https://kbarker.dev/blog/event-delegation/">my preferred approach to event delegation</a>. You can <a href="https://kbarker.dev/blog/get-the-common-ancestor-of-two-or-more-elements-in-vanilla-js/">read the blog post</a> for more information.</p>

<p id="output" aria-live="polite"></p>

<div>
  <button type="button">A</button>
  <button type="button">B</button>
  <button type="button">C</button>
  <button type="button">D</button>
  <button type="button">E</button>
  <button type="button">F</button>
  <button type="button">G</button>
  <button type="button">H</button>
  <button type="button">I</button>
  <button type="button">J</button>
  <button type="button">K</button>
  <button type="button">L</button>
  <button type="button">M</button>
  <button type="button">N</button>
  <button type="button">O</button>
  <button type="button">P</button>
  <button type="button">Q</button>
  <button type="button">R</button>
  <button type="button">S</button>
  <button type="button">T</button>
  <button type="button">U</button>
  <button type="button">V</button>
  <button type="button">W</button>
  <button type="button">X</button>
  <button type="button">Y</button>
  <button type="button">Z</button>
</div>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  width: 88%;
  max-width: 30em;
  margin: 1em auto;
  line-height: 1.5;
  font-family: Arial, sans-serif;
}

div {
  display: flex;
  flex-wrap: wrap;
}

button {
  width: 50px;
  height: 50px;
  margin: .25em;
  font: inherit;
}
              
            
!

JS

              
                //
// Variables
//

// Get the #output element
const output = document.querySelector('#output');

// Save the selector for the buttons
const selector = 'button';


//
// Functions
//

/**
 * Get the common ancestor of two or more elements
 * {@link https://gist.github.com/kieranbarker/cd86310d0782b7c52ce90cd7f45bb3eb}
 * @param {String} selector A valid CSS selector
 * @returns {Element} The common ancestor
 */
function getCommonAncestor (selector) {
  // Get the elements matching the selector
  const elems = document.querySelectorAll(selector);

  // If there are no elements, return null
  if (elems.length < 1) return null;

  // If there's only one element, return it
  if (elems.length < 2) return elems[0];

  // Otherwise, create a new Range
  const range = document.createRange();

  // Start at the beginning of the first element
  range.setStart(elems[0], 0);

  // Stop at the end of the last element
  range.setEnd(
    elems[elems.length - 1],
    elems[elems.length - 1].childNodes.length
  );

  // Return the common ancestor
  return range.commonAncestorContainer;
}


//
// Inits & Event Listeners
//

// Attach a delegated event handler
// to the closest common ancestor of the button elements
getCommonAncestor(selector).addEventListener('click', function (event) {
  // Bail if the event target doesn't match the selector
  if (!event.target.matches(selector)) return;

  // Show which button the user clicked
  output.textContent = `You clicked: ${event.target.textContent}`;
});
              
            
!
999px

Console