Side navigation
#4231 closed feature (invalid)
Opened February 24, 2009 03:24PM UTC
Closed November 17, 2010 01:26AM UTC
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: |
Description
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):
$.fn.extend(
/**
- 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;
}
});
Attachments (0)
Change History (4)
Changed February 24, 2009 03:32PM UTC by comment:1
Changed February 24, 2009 04:10PM UTC by comment:2
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
Changed March 01, 2009 06:33PM UTC by comment:3
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.
Changed November 17, 2010 01:26AM UTC by comment:4
resolution: | → invalid |
---|---|
status: | new → closed |
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.
if this is something which would be considered for jquery further optimization could be:
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?