Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

get-element-selectors

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

get-element-selectors

Get element CSS selectors

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

get-element-selectors

Returns an array of CSS selector strings for a given DOM element.

Usage

getElementSelectors(element, option)

Example

import { getElementSelectors } from 'get-element-selectors';

const option =  { attributes: ['name', 'type', 'data-foo'], maxResults: 10 }

const ele = document.querySelectorAll("div")[3];

const selectors = getElementSelectors(element, option);

console.log(selectors); // Output: ['.target', ...]

Parameters

element

This is the HTML Element.

option

Option is an Object with the following fields:

  1. attributes : This is an array containing attribute names can be used as selectors. Default is ['type', 'name'] .
  2. maxResults : This sets the maximum number of selector strings will return

Types

export type GetElementSelectorsOption = {
  attributes: string[];
  maxResults: number;
};

export const getElementSelectors = (
  element: Element,
  option: GetElementSelectorsOption = { attributes: ['type', 'name'], maxResults: 10 },
): string[]  

Notes

  1. If an element has an ID attribute, the id selector will be the only selector in the returned array.
  2. Only valid class names will be used in CSS selectors.

Keywords

FAQs

Package last updated on 05 Aug 2020

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