3.0.2 • Published 4 years ago

element-closest v3.0.2

Weekly downloads
165,447
License
CC0-1.0
Repository
github
Last release
4 years ago

closest

closest is a polyfill for #Element.closest.

npm install element-closest

The #Element.closest method returns the closest element that matches a selector. It returns the element itself, one of its ancestor, or null if there isn't any match.

element.closest(selectorString) //=> Element or null

This is especially useful for delegating events.

document.addEventListener('click', function (event) {
  // find nearest element up the tree that is an <a>
  var link = event.target.closest('a');

  if (link) {
    // do something with the <a>
    event.preventDefault();
  }
});

The script is approximately 428 bytes, or 257 bytes when gzipped.

Usage

For immediate usage, add this script to your document:

<script src="https://unpkg.com/element-closest"></script>

For usage in Node, including Browserify and Webpack, run closest with your window object:

const elementClosest = require('element-closest');

elementClosest(window); // this is used to reference window.Element

Browser compatibility

BrowserNative SupportPolyfill Support
Android532.2+
Blackberry7+
Chrome41+4 - 40
Edge1512 - 14
Firefox35+3.5 - 34
Internet Explorer8+
Opera28+10 - 27
Opera Mobile37+12+
Safari (iOS)9.2+3.2 - 8.4
Safari (MacOS)9.1+3.1 - 8

Internet Explorer 8

closest is especially useful for delegating events, but remember that Internet Explorer 8 does not support #Element.addEventListener.

matches

This library also polyfills #Element.matches, which is widely supported but often vendor-prefixed.

element.matches(selectorString) //=> boolean

matches is especially useful for short-handing hasAttribute or classList.contains with selectors.

const widget = document.querySelector('.custom-widget');

if (widget.matches('[data-active]') || widget.matches('.widget--active')) {
  // do something with the active widget
}
@knapsack-cloud/msk-design-systemnlisgo-pattern-library@plarin/build-configs@plarintest/build-configsplayer-reactdemio-browser-viewerlms-system-service-frontedsectionizeng2-semantic-ui-v12@everything-registry/sub-chunk-1567fe-dockerfhmapfpc-uifront.jsember-cli-element-closest-polyfillember-paper-liteember-paperember-paper-exes6-delegatehyperdom-interactjs@giomamaladze/ng2-semantic-uiirisplus-components-test@hundh/contao-utils-bundle@hochzehn/ng2-semantic-ui@entdec/satis@everfund/sdkgp-boilerplate@ffc-sync/theme@ffw/randstad-local-orbithcmui.js@fmsim/builderkksng-uijwtbutlerkorus-uilacront-core-components@knapsack-cloud/msk-pattern-garden-javascript@sberbusiness/triplexns-otp-ui@rzyman/semantic-ui@reltio/search@richardlt/ng2-semantic-ui@orioro/web-ui-core@orioro/website-ui-components@packaged-ui/ie-compat@stimulus/polyfillspayhere-embed-sdkngxp-fomantic-uing-yavorskiy-uingx-ng2-semantic-uingx-fomantic-uingx-billboardng2-semantic-uing2-semantic-ui-cloneng2-semantic-ui-v9ngx-althares-ui@pluralsight/ps-design-system-drawer@project-sunbird/ng2-semantic-uinhsuk-react-commenting-component@samiyev/ngx-billboardobject-fit@sgu/ng2-semantic-uipilasbloques-ember-paperlivedatelumen-cms-corelumen-cms-base@magoniac/ngx-fomantic-ui@near-solution/builder@pattern-lab/uikit-polyfillsng-lower-code-ui@piratuks/ng-semantic-uiquieligendi@telsystems/build-configsprimeng-px@systembug/ember-paper@tannerhodges/match-height@tannerhodges/snap-slider@things-factory/builderteleport-autocomplete@appdirect/universal-header@angular-ex/semantic-ui@aligorji/ng2-semantic-ui@alphakits/ui@alfalab/core-components-gallery@alfalab/core-components@alfalab/core-components-notificationcadre-componentssui-tags-input@botxo_team/fe-sharedumbertorc-year-picker@wisdoj/ng2-semantic-uiwq-felsilc-accordionyuzu-polyfillsyearpickerxvalinearcadia-browser-shimsappril-polyfillsweui.jsweui.ug.js
3.0.2

4 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.0.2

7 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.0.3

9 years ago

1.0.2

9 years ago