JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 12
  • Score
    100M100P100Q42504F
  • License ISC

Vanilla javascript version of Jquery's parentsUntil function

Package Exports

  • parentsuntil

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (parentsuntil) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Build Status

parentsUntil

This is a packaged version of the Chris Ferdinandi utility function that replicates the functionality of the Jquery function of the same name.

Installation

npm i parentsuntil

Usage

import parentsUntil from 'parentsUntil'

var elem = document.querySelector('#some-element');
var parentsUntil = parentsUntil(elem, '.some-class');
var parentsUntilByFilter = parentsUntil(elem, '.some-class', '[data-something]');

Real world example

Add an is-active class to the parent .nav__item when a menu item is clicked

<div class="nav">
  <ul class="nav__submenu">
    <li class="nav__item">
      <a href="#">nav item</a>
      <ul class="nav__submenu">
        <li id="test" class="nav__item">
          <a href="#">sub item</a>
        </li>
      </ul>
    </li>
  </ul>
</div>

document.getElementById('test').addEventListener('onClick', onClick)

onClick(event) {
  const navItem = event.target
  const parents = parentsUntil(navItem, '.nav')
  parents.forEach(DOMNode => {
    if (DOMNode.classList.contains('nav__submenu')) {
      DOMNode.parentNode.classList.add('is-active')
    }
  })
}