Bug Tracker

Opened 10 years ago

Closed 8 years ago

#4231 closed feature (invalid)

Add new "path" method which returns selector path to jQuery object

Reported by: strykker Owned by: john
Priority: minor Milestone: 1.4
Component: selector Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:


It would be nice to have a function in the code to generate a 'query' selector path to any element from any containing element. Sort of like a jQuery equivalent of an xpath. I put together the following as an example of what it might look like (can probably be made more efficient .. and I'm not sure if using eq() provides the fastest selector path when resolved):



  • Return a valid jQuery selector path to an object
  • from a container
  • root - specify a root element from which to generate
  • the path *
  • - If no root is specified, then path is given from
  • the document root
  • If root is same as element, no path is returned
  • (undefined)
  • If root is not actually a parent of this element,
  • then no path is returned (undefined); * */

path : function(root) {

var r; if(root) {

r = $(root)[0];

} else {

r = $()[0];


var el = this[0];

if(el) {

var path = ""; while(el && el.parentNode && el != r) {

if(el.nodeType == 9) { return;} reached document node, no

root provided (probably, b/c given root is not a valid parent of this node)

path = " > " + el.tagName.toLowerCase() + ":eq(" + $(el).prevAll

(el.tagName).size() + ") " + path;

var el = el.parentNode;


} return path;



Change History (4)

comment:1 Changed 10 years ago by Markus.Staab

if this is something which would be considered for jquery further optimization could be:

  • search up to root until found an element with an id. build a selector starting from this #id up to the actual element.

but im not sure when such a function would be useful at all.. why is the path to the element is needed if you already have the element?

comment:2 Changed 10 years ago by strykker

It's very useful for storing a path to an element for future use.

It's basically like storing a query to an element or an xpath in an XML doc which can be evaluated at a later to to return the same element (assuming the document structure hasn't changed). I am using this at the moment for just this.

  • Eric

comment:3 Changed 10 years ago by bentruyman

Why can't you just store the element you want to refer back to later in a variable? This is something I do quite often, that is, store pointers to DOM elements for later use.

var menu = {};
menu.container = $('#myMenu').get(0);
menu.buttons = $('a', menu.container).get();

This way you have direct pointers to the elements, and if their placement changes over time, these pointers still work -- whereas if you had a path method that generated a path for you, it would break if the elements moved.

comment:4 Changed 8 years ago by dmethvin

Resolution: invalid
Status: newclosed

There is no guaranteed selector string that can be used to access elements in the future, since methods like .filter(function) have no selector string equivalent. Save a reference to the DOM elements if it's too expensive to recompute.

Note: See TracTickets for help on using tickets.