npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

validate-target

v3.1.1

Published

Easily validate target of an HTML element especially during event delegation

Downloads

4,270

Readme

validateTarget

GitHub Workflow Status (branch) Coverage Status

validate-target is a minimalist script to easily validate target of an HTML element especially during event delegation. Event delegation is advised for better performance, read MDN documentation for more information.

No needs to check nodeName or className properties, the function return directly a boolean when the target is correct.

The target can have multiple node name if element shares common code. The function use Element.matches to ensure selectorString match the target.

Installation

The library is available as the validate-target package name on npm and Github.

npm install validate-target --save-dev
yarn add validate-target --dev

Warning validate-target@3 is ESM.

Note Minimum supported Node.js version is 16.20.0.

CDN

You can also download it and include it with a script tag. The library will be registered as the global variable window.validateTarget.

<script src="https://cdn.jsdelivr.net/npm/validate-target@3" crossorigin></script>

Note You can browse the source of the NPM package at jsdelivr.com/package/npm/validate-target.

How it works

Basic usage

The following example returns true because the nodeName and selectorString properties match.

<a href="" class="itemLink">Link</a>
validateTarget({
  target: document.querySelector('.itemLink'),
  selectorString: '.itemLink',
  nodeName: 'a'
});

Multiple node names

The following example returns true because button is a matched value for nodeName and selectorString.

<a href="" class="itemLink">Link</a> <button class="itemButton">Button</button>
validateTarget({
  target: document.querySelector('.itemLink'),
  selectorString: '.itemLink',
  nodeName: ['button', 'a']
});

Event delegation

The following example creates a click event listener on the nav element and intercepts click events on the itemLink element.

<nav class="nav">
  <ul>
    <li>
      <a href="" class="itemLink">Link</a>
    </li>
    <li>
      <a href="" class="itemLink">Link</a>
    </li>
    <li>
      <a href="" class="itemLink">Link</a>
    </li>
  </ul>
</nav>
document.querySelector('.nav').addEventListener('click', (e) => {
  const validateTargetLinkClick = validateTarget({
    target: e.target,
    selectorString: '.itemLink',
    nodeName: 'a'
  });

  if (validateTargetLinkClick) {
    console.log('Link is clicked.');
  }
});

Parameters

target

Type:

type target = HTMLElement;

Tells to the function the target element.

selectorString

Type:

type target = string;

Tells the function the selector string of the target element. Can be any valid CSS selector (class, id, attribute, etc.). The function uses the Element.matches function.

nodeName

Type:

type target = string | string[];

Tells the function the node names. The order of the parameters in the array does not matter.

License

validate-target is licensed under the MIT License.

Created with ♥ by @yoriiis.