# unique-selector

> Given a DOM node, return a unique CSS selector matching only that element

Latest version **0.5.0** (published 2020-10-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install unique-selector
pnpm add unique-selector
yarn add unique-selector
bun add unique-selector
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.0 |
| Published | 2020-10-13 |
| First published | 2013-03-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 190 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 272 |
| Author | Eric Clemmons |
| Maintainers | ericclemmons, avrmav |
| Keywords | dom, html, css, selector, event |

## Links

- npm: https://www.npmjs.com/package/unique-selector
- Repository: https://github.com/ericclemmons/unique-selector
- Homepage: https://github.com/ericclemmons/unique-selector#readme
- Issues: https://github.com/ericclemmons/unique-selector/issues
- npm.io page: https://npm.io/package/unique-selector

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 0.5.0 (latest) — 2020-10-13
- 0.4.1 — 2018-11-19
- 0.4.0 — 2018-11-19
- 0.3.6 — 2017-07-06
- 0.3.5 — 2017-05-25
- 0.3.4 — 2017-02-20
- 0.3.3 — 2017-02-20
- 0.3.2 — 2017-02-20
- 0.3.1 — 2017-02-15
- 0.3.0 — 2016-11-17
- 0.1.2 — 2016-11-07
- 0.1.1 — 2016-04-23
- 0.1.0 — 2016-04-12
- 0.0.4 — 2014-10-26
- 0.0.2 — 2013-03-27

## README

unique-selector
===============

[![Build Status](https://travis-ci.org/ericclemmons/unique-selector.png)](https://travis-ci.org/ericclemmons/unique-selector)
[![CocoaPods](https://img.shields.io/cocoapods/l/AFNetworking.svg)]()
[![semantic-versioning](https://img.shields.io/badge/semantic%20-versioning-green.svg)]()

Given a DOM node, return a unique CSS selector matching only that element.
This is particularly useful when tracking in custom variables in analytics:


    document.addEventListener('click', function(event) {
        var selector = unique(event.target);

        _gaq.push(['_trackEvent', 'Engagement', 'Click', selector]);
    }, false);


Installation
------------

[![NPM](https://nodei.co/npm/unique-selector.png?mini=true)](https://nodei.co/npm/unique-selector/)

Options
------------
e.g.1 DomElement = `<span id="test"></span>`

```
import unique from 'unique-selector';

// Optional Options
options = {
    // Array of selector types based on which the unique selector will generate
    selectorTypes : [ 'ID', 'Class', 'Tag', 'NthChild' ]
}

unique( DomElement, options ); // #test
```

e.g.2 DomElement = `<span test="2"></span>`

```
import unique from 'unique-selector';

// Optional Options
options = {
    // Array of selector types based on which the unique selector will be generate
    selectorTypes : [ 'Attributes' ]
}

unique( DomElement, options ); // [test="2"]
```

e.g.3 DomElement = `<div id="xyz" class="abc test"></div>`

```
import unique from 'unique-selector';

// Optional Options
options = {
    // Regular expression of ID and class names to ignore
    excludeRegex : RegExp( 'xyz|abc' )
}

unique( DomElement, options ); // .test
```


Tests
-----

    $ npm run test


Contributing
-----
Feel free to open issues, make suggestions or send PRs.
This project adheres to the Contributor Covenant [code of conduct](http://contributor-covenant.org/). By participating, you are expected to uphold this code.


Contact
-----

Avraam Mavridis : [@avraamakis](https://twitter.com/avraamakis)

Eric Clemmons : [@ericclemmons](https://twitter.com/ericclemmons)


Releases
--------
- v0.1.0

    - Big refactor/rewrite using es6
    - More test + Change the test script
    - Script to compile es6 to es5 using babel
    - Ability to pass options for the **selectorTypes** based on which the unique selector will be generated, see the options


- v0.0.4

    - Handle extra whitespace in `className` ([#9](https://github.com/ericclemmons/unique-selector/pull/9))

- v0.0.3

    - Add support for various `<form>` elements ([#2](https://github.com/ericclemmons/unique-selector/issues/2))

- v0.0.2

    - Fix bug with `nth-child` calculation

- v0.0.1

    - Initial release



License
-------

MIT


[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/AvraamMavridis/unique-selector/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

---
_Source: https://npm.io/package/unique-selector · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
