I need to generate unique css selector for elements.
Particularly, I have onclick event handler that should remember what target element
was clicked and send this info to my server. Is there a way to do it without doing DOM modifications?
P.S. my javascript code supposed to be run on different
3-rd party websites so I can’t make any assumptions about html.
let say you have a list of links for the sake of simplicity: you can simply pass the index of the triggering element in the collection of all elements
the js (jQuery 1.7+, I used
.on()otherwise usebind()) function can beso that if you click on third element index value passed will be 2. (0-based index);
of course this is valid as long as the code (the DOM) doesn’t change. Later you can use that index to create a css rule to that element e.g. using
:nth-childOtherwise if each one of your elements have a different attribute (like an id) you can pass that attribute
example on JsFiddle : http://jsfiddle.net/t7J8T/