View Single Post
Old 02-19-2013, 03:40 PM   PM User | #1
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
DOM transversal going upwards

so, in jQuery there's a transversal method called closest:
http://api.jquery.com/closest/

which you can supply a selector and it will travel up the DOM until it finds an element that matches.

in my case, I need to find the contenteditable div that a text node belongs to, so in jquery it would be something like:
Code:
$(this).closest('div[contenteditable="true"]')
I can't use jQuery in the code that I'm working on, but I need that functionality - the text node may be nested inside other elements, so doing parentNode.parentNode, etc is unreliable.

It only has to work on firefox, so query selectors were looking promising, but they travel down from the specified element - I need something that travels up.

Does anybody know of anything in vanilla js that does something similar to jQuery's closest(), or am I stuck with walkin' the tree, or is there some other possible solution?

thanks in advance...
xelawho is offline   Reply With Quote