3.0.2 • Published 6 years ago

element-closest v3.0.2

Weekly downloads
165,447
License
CC0-1.0
Repository
github
Last release
6 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
}
nlisgo-pattern-library@plarin/build-configs@plarintest/build-configsplayer-reactdemio-browser-viewerlms-system-service-frontedsectionizeng2-semantic-ui-v12@everything-registry/sub-chunk-1567weui.jsweui.ug.jswq-felxiaojijiteleport-autocompletesui-tags-inputv-ng2-semantic-uiv-ng2-semantic-ui-v9uturnv-sb-semantic-uiv-semantic-ui-sbveem-web-sdkvanilla-select@botxo_team/fe-sharedappril-polyfillsirisplus-components-testjwtbutlerkksng-uilacront-core-componentshyperdom-interactjshcmui.jslivedatelumen-cms-baselumen-cms-corekorus-ui@winnyboy5/ng2-semantic-ui@wisdoj/ng2-semantic-ui@workday/canvas-kit-react-core@things-factory/builder@telsystems/build-configs@valery.vs/ng2-semantic-ui-select@yhnavein/ng2-semantic-uiamannn-caritas-online-beratung-frontend@zojeda/ng2-semantic-uiaeopus-pattern-libraryairbnb-browser-shimsalfalab-core-components-fixedab-ng2-semantic-uiaccessiblenav.jsaktis-ng2-semantic-uiau-json-schema-form-sampleaxiom-components-buildarcadia-browser-shims@alphakits/ui@alfalab/core-components@alfalab/core-components-notification@alfalab/core-components-gallery@aligorji/ng2-semantic-uiumbertotui-tags-input@carrotwu/valine@catapult-tech/cp-design-system-drawer@cable_ready/polyfills@brandwatch/axiom-components@bolt/coreyearpickerxvalineyuzu-polyfillszillow-browser-shimszweui.jsvue-weui-xvalinevaline-gnasvaline-memfirevaline-unpkg-avminzkui-weui@everfund/sdk@near-solution/builder@rzyman/semantic-ui@project-sunbird/ng2-semantic-ui@samiyev/ngx-billboard@sberbusiness/triplex@reltio/search@pluralsight/ps-design-system-drawer@pattern-lab/uikit-polyfills@piratuks/ng-semantic-ui@richardlt/ng2-semantic-uins-otp-uiobject-fitpayhere-embed-sdkrc-year-pickerrosemary-uisegi-ant-betang-yavorskiy-uing2-semantic-uing2-semantic-ui-cloneng2-semantic-ui-v9ngx-billboardngxp-fomantic-uingx-fomantic-uingx-ng2-semantic-ui
3.0.2

6 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.2

9 years ago

2.0.1

10 years ago

2.0.0

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago