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>

<form>
  <p>
    <label for="name">Name</label>
    <input id="name" type="text" name="name">
  </p>
  <p>
    <label for="email">Email</label>
    <input id="email" type="email" name="email">
  </p>
  <p>
    <label for="message">Message</label>
    <textarea id="message" name="message"></textarea>
  </p>
  <p>
    <button type="submit">Submit</button>
  </p>
</form>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

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

label, input, textarea {
  display: block;
  width: 100%;
}

input, textarea, button {
  font: inherit;
}
              
            
!

JS

              
                //
// Variables
//

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

// Save the selector for the form fields
const selector = 'input, textarea';


//
// 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 form fields
getCommonAncestor(selector).addEventListener('focusout', function (event) {
  // Bail if the event target doesn't match the selector
  if (!event.target.matches(selector)) return;

  // Bail if the value is empty
  if (!event.target.value.trim()) return;

  // Show what the user typed
  output.textContent = `You typed: ${event.target.value}`;
});
              
            
!
999px

Console