You can also be interested in:
What if you need to get the css selector which targets a DOM element given the element itself?
This is the case for example when you want to store some added information over an element and show it after recharging the page. In this case you may store the information in the local storage object together with the path to the element, in other words you must be able to store the element position inside the DOM
In this scenario for example you have the user choosing the element with a click event, ask him for input some information and then store the information and the path to the element, so that when recharging the page the information can be tied to the element selected.
To get this you can use the following js function I wrote. Only some requirements here:
- the function requires the mootools selector engine
- the function works in browsers which support the nth-of-type pseudo-selector
Here comes the function