views:

73

answers:

2

Afternoon peoples.

I am having a bit of a nightmare traversing a DOM tree properly. I have the following markup

http://pastie.org/999519

... And I am trying to traverse from input.save at the bottom right the way up to div.node... This all works well with one copy of the markup but if I duplicate it (obvisouly changing the ID of the uppermost div.node and use jQuery.closest('div.node') for the upper of the div.node's it will return the element below it not the element above it (which is the right one). I've tried using parents() but that also has it's caveats. Is there some kind of contexyt that can be attached to closest to make it go up and not down? or is there a better way to do this.

Thanks in advance.

Alex

P.S It seems that stackoverflow has done something weird to the markup! - I just triple checked it and it is fine but for some reason it's concate'd it below

A: 

First of all, you really should use an id for the .save element, unless you have more, and if so then use a node prefix if you can, so $('input.save')...

I'm not sure what the problem is with your code. You say it is traversing down when you want it to go up? It is supposed to go up.

From jQuery docs at http://api.jquery.com/closest/:

Get the first ancestor element that matches the selector, beginning at the current element and progressing up through the DOM tree

Maybe there is some problem with using .find() right after closest(). However, I don't think that would be a problem.

Dale
if you read the docs at jQuery they state it goes down not up (in the comments)...I assumed it would go up aswel but it doesnt!.I know about adding input.save etc etc.. this is just POC for something
Alex
A: 

Sorry for any time wasted...

I fixed the problem by moving the markup around to suit a better option.

Thanks for all the input guys / gals

Alex