Get all elements like myself in jQuery?
javascript, jquery, jquery-plugins, jquery-selectors
Solution
This is my new try, a `.me()` method that will return you the selector of the current element built up until the `body`.
$.fn.me = function(){
return this.first().parentsUntil("body").andSelf().map(function(){
return this.tagName;
}).get().join(">");
};
Usage:
var selector = $(this).me();
var $twins = $(selector);
See it here.
An alternative version that accepts a root element as an argument:
$.fn.me = function(root){
return this.first().parentsUntil(root).andSelf().map(function(){
return this.tagName;
}).get().join(">");
};
Usage:
var selector = $(this).me("#root");
var $twins = $(selector, "#root");
See it here.
A third version that keeps the relative position of the descendants of the topmost element under the root element. For example, it will return `DIV>DIV:nth-child(1)>SPAN:nth-child(2)` instead of a generic `DIV>DIV>SPAN` selector.
$.fn.me = function(root){
return this.first().parentsUntil(root).andSelf().map(function(){
var eq = i ? ":nth-child(" + ($(this).index() + 1) + ")" : "";
return this.tagName + eq;
}).get().join(">");
};
Usage:
var selector = $(this).me("#root");
var $twins = $(selector, "#root");
Problem
Lets say I have this structure ( no classes , no Id's): - it's a location finding question The d[0,1,2] are pure `divs` . no class no id. div (`d0`) wrapps a repeating structure like : And let's say I have `$(this)` as the first `d1` Is there any traverse function , method , solution for getting the other 2 `d1` ? ( or all `d1's`.it doesn't matter..) I'll prase it with words : I'm `$(this)=d1`. I have a parent ( which is `d2` , can be more ....this is just for my sample) which has a parent (`d3` ) and hence, I have 2 more exact elements like me. how can I get them ? JSBIN edit Maybe I wasn't clear but I don't have to know the structure. I just have `$(this)` and it should find by itself other twins relative to `d0` ( as same as its position) so it should be something like : ``` function getLikeMyself(objWhichIsThis , contextDivElement) { } execute : getLikeMyself(theDivWhishIsThis, divElemtnWhichIs_d0) ```