Welcome to WebmasterWorld Guest from 35.153.73.72

Forum Moderators: open

Message Too Old, No Replies

jquery incorrect index of body child

error at the end of traversing "chain"

     
9:26 am on Nov 14, 2018 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2003
posts: 378
votes: 5


I've been trying to create a chain of indexes from the given element up to the body element:

result = $(given_element).parents().map(function(){ return $(this).index();}).get().reverse().join(",") + result;


another code (explored before the above):
var result = "";
given_element.parents().each(function() { result = $(this).index() + "," + result;});


The example of the result: "12,2,0,0,1,0,0,1,0"

and the result of my php script (based on PHP Simple html parser) which performs the same task is: "0,2,0,0,1,0,0,1,0,0" (This is the correct result )

The jquery produced wrong result always has wrong huge numbers at the beginning of the chain (12 here, may be 72, 16, 22... etc... in other cases but it's constant for every given page with the given element)

All other chains produced (e.g. tagName, element.prop...) are OK in both scripts.

Whats wrong with the "index()" of body element children? Or with me? ;)
2:58 pm on Nov 14, 2018 (gmt 0)

Preferred Member

10+ Year Member

joined:Jan 4, 2003
posts: 378
votes: 5


I've decided to create a temp. patch converting <body> to <body><div id="my_jquery_patch"> (and </div></body>, of course) and then replacing the 1-st number with comma on empty string in the result.
Works fine, and the js and php parsers' results are now 100% identical. But I'd be glad to find the answer too.