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
}
@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-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-componentsumbertotui-tags-input@appdirect/universal-header@angular-ex/semantic-ui@alfalab/core-components@alfalab/core-components-notification@alfalab/core-components-gallery@alphakits/ui@aligorji/ng2-semantic-ui@carrotwu/valine@catapult-tech/cp-design-system-drawer@cable_ready/polyfills@brandwatch/axiom-components@bolt/core@decisiv/ui-components@department-of-veterans-affairs/formation@devsisters/gatsby-plugin-autotrackyearpickerxvalineyuzu-polyfillszillow-browser-shimszweui.jsvue-weui-xvalinevaline-gnasvaline-memfirevaline-unpkg-avminzkui-weui@hochzehn/ng2-semantic-ui@hundh/contao-utils-bundle@ffc-sync/theme@ffw/randstad-local-orbit@everfund/sdk@fulcanellee/core-components-sandbox@fmsim/builder@giomamaladze/ng2-semantic-ui@knapsack-cloud/msk-pattern-garden-javascript@magoniac/ngx-fomantic-ui@near-solution/builder@entdec/satis@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@orioro/website-ui-components@orioro/web-ui-core@packaged-ui/ie-compat@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-uing-lower-code-uingx-althares-uinhsuk-react-commenting-componentpilasbloques-ember-paperquieligendi
3.0.2

6 years ago

3.0.1

6 years ago

3.0.0

6 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.3

10 years ago

1.0.2

10 years ago