Socket
Socket
Sign inDemoInstall

find-parent

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

find-parent

A utility for finding the nearest parent that matches a test function


Version published
Weekly downloads
1.1K
increased by39.49%
Maintainers
1
Weekly downloads
 
Created
Source

find-parent

Build Status devDependency Status

Find-parent is a utility to help find the closest element up an element's parent tree (possibly including itself) that matches certain rules.

Installation

$ npm install find-parent --save-dev

Usage

The examples below will use this as an example DOM structure.

<div class="foo">
  <span id="test" data-test-node="test">
    <a href="http://google.com">link text</a>
  </span>
</div>

byMatcher(element, func, opts)

var findParent = require('find-parent');

var element = document.getElementsByTagName('a')[0];

var result = findParent.byMatcher(element, function(node) {
  return node.className === 'foo';
});

// result is === to the element <div class="foo">

byClassName(element, className, opts)

var findParent = require('find-parent');

var element = document.getElementsByTagName('a')[0];

var result = findParent.byClassName(element, 'foo');

// result is === to the element <div class="foo">

withDataAttribute(element, attName, opts)

var findParent = require('find-parent');

var element = document.getElementsByTagName('a')[0];

var result = findParent.withDataAttribute(element, 'testNode');

// result is === to the element <span id="test" data-test-node="test">

Options

All findParent functions take an optional options argument.

keyvalue typedefaultdescription
throwOnMissbooleanfalseThrow error if no matching parent is found

Keywords

FAQs

Package last updated on 01 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc