Given a DOM node, return a unique CSS selector matching only that element
JavaScript
Latest commit 66e8f2b Sep 14, 2017 @techmexdev techmexdev committed with AvraamMavridis chore: Update README.md (#30)
Permalink
Failed to load latest commit information.
src fix: classList.toString don't remove extra whitespace characters (#29) Jul 4, 2017
static
test fix: classList.toString don't remove extra whitespace characters (#29) Jul 4, 2017
.babelrc fix: classList.toString don't remove extra whitespace characters (#29) Jul 4, 2017
.eslintrc
.gitignore chore: Add yarn.lock to .gitignore Nov 7, 2016
.npmignore Add .npmignore Nov 7, 2016
.travis.yml
README.md chore: Update README.md (#30) Sep 14, 2017
component.json Initial component + test Dec 11, 2012
package.json

README.md

unique-selector

Build Status CocoaPods semantic-versioning

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

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"]

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. By participating, you are expected to uphold this code.

Contact

Avraam Mavridis : @avraamakis

Eric Clemmons : @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)
  • v0.0.3

    • Add support for various <form> elements (#2)
  • v0.0.2

    • Fix bug with nth-child calculation
  • v0.0.1

    • Initial release

License

MIT

Bitdeli Badge