https://github.com/ericclemmons/unique-selector
Given a DOM node, return a unique CSS selector matching only that element
https://github.com/ericclemmons/unique-selector
Last synced: about 1 year ago
JSON representation
Given a DOM node, return a unique CSS selector matching only that element
- Host: GitHub
- URL: https://github.com/ericclemmons/unique-selector
- Owner: ericclemmons
- License: mit
- Created: 2012-12-10T21:25:55.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2023-10-16T15:20:31.000Z (over 2 years ago)
- Last Synced: 2025-05-12T05:52:18.902Z (about 1 year ago)
- Language: JavaScript
- Size: 209 KB
- Stars: 267
- Watchers: 5
- Forks: 57
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
unique-selector
===============
[](https://travis-ci.org/ericclemmons/unique-selector)
[]()
[]()
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
------------
[](https://nodei.co/npm/unique-selector/)
Options
------------
e.g.1 DomElement = ``
```
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 = ``
```
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 = `
`
```
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 `` 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
[](https://bitdeli.com/free "Bitdeli Badge")