Code Monkey home page Code Monkey logo

css-selector-generator's Introduction

CSS Selector Generator

JavaScript object that creates a unique CSS selector for a given DOM element.

It should work fine in any modern browser. It has no external dependencies.

It also generates shorter selectors and is faster and/or more robust than many other libraries - see this comparison and select the best alternative for your use case.

How to use

// first, create instance of the object with default options
my_selector_generator = new CssSelectorGenerator;

// create (or find reference to) any element
my_element = document.createElement('div');
document.body.appendChild(my_element);

// then you can get unique CSS selector for any referenced element
my_element_selector = my_selector_generator.getSelector(my_element);

The most common use case is finding a unique CSS selector for any referenced element. This is handy if you, for example, let your users select any element on the page by clicking on it:

// track every click
document.body.addEventListener('click', function (event) {
  // get reference to the element user clicked on
  var element = event.target;
  // get unique CSS selector for that element
  var selector = my_selector_generator.getSelector(element);
  // do whatever you need to do with that selector
  console.log('selector', selector);
});

Options

You can set the options either when creating an instance, or via the setOptions() method:

custom_options = {selectors: ['tag', 'id', 'class']};

// set options when creating an instance
my_selector_generator = new CssSelectorGenerator(custom_options);

// or set options later
my_selector_generator.setOptions(custom_options);

selectors

default: ['id', 'class', 'tag', 'nthchild']

So far the only option available is the list of types of selectors that will be used when constructing the unique CSS selector. You may want to adjust this list for browser compatibility.

NOTE: The generator keeps the order of selectors when testing for their uniqueness. So if you will set ['class', 'id'] and the element will have unique classname and ID, the resulting selector will contain classname, even if ID is stronger. Also, keep the nthchild selector in last place, because it always generates unique selector and it will prevent using any other selector type behind it.

Available values:

  • 'tag' - Tag selector. E.g. p, div.
  • 'id' - ID selector. E.g. #myElement.
  • 'class' - Class selector. It will get all class names of the element. E.g. .myClass, .firstClass.secondClass.
  • 'nthchild' - N-th child selector. It is supported by IE9 and higher, but it is necessary to create a unique CSS selector for every possible element. You can remove it from the list for backwards browser compatibility, but then make sure to use IDs or class names on each element you want to target. E.g. :nth-child(0)
  • 'attribute' - Attribute selector. Compatible wth IE7 and higher. It will not create matching pairs for element's ID and class name attributes. This type of selector is disabled by default. E.g. [rel=someRel]

id_blacklist

default: []

List of ID values, which will be ignored when creating ID selectors.

class_blacklist

default: []

List of class names, which will be ignored when creating class selectors.

prefix_tag

default: false

If set to true, ID selectors will be prefixed with the tag name.

For example selector for this element

<div id="myElement">...</div>

will look like this: div#myElement.

attribute_blacklist

default: []

List of attribute names, which will be ignored when creating attribute selectors. Attributes "id" and "class" are always be ignored.

attribute_whitelist

default: []

List of attribute names, which will be prioritised when creating attribute selectors.

quote_attribute_when_needed

default: false

If set to true, attribute selector values will be quoted and escaped when needed.

Bug reports, feature requests and contact

If you found any bugs, if you have feature requests or any questions, please, either file an issue on GitHub or send me an e-mail at [email protected]

License

CSS Selector Generator is published under the UNLICENSE license. Feel free to use it in any way.

css-selector-generator's People

Contributors

dandv avatar fczbkk avatar indilo53 avatar joscha avatar kdmurthy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.