Returns an array of CSS selector strings for a given DOM element.
getElementSelectors(element, option)
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', ...]
element
This is the HTML Element.
option
Option is an Object with the following fields:
attributes
: This is an array containing attribute names can be used as selectors. Default is['type', 'name']
.maxResults
: This sets the maximum number of selector strings will return
export type GetElementSelectorsOption = {
attributes: string[];
maxResults: number;
};
export const getElementSelectors = (
element: Element,
option: GetElementSelectorsOption = { attributes: ['type', 'name'], maxResults: 10 },
): string[]
- If an element has an ID attribute, the id selector will be the only selector in the returned array.
- Only valid class names will be used in CSS selectors.