Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
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 454 Times in 452 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
Old 02-19-2013, 05:03 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 959
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by xelawho View Post
I need to find the contenteditable div that a text node belongs to
I would have thought this would do it:
Code:
function getEditableParent( elem )
{
  while( elem && !elem.parentNode.contentEditable )
    elem = elem.parentNode;
 
  return  elem ? elem.parentNode : null;
}
Logic Ali is offline   Reply With Quote
Old 02-19-2013, 05:14 PM   PM User | #3
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
that would... I'm always a little nervous about while loops, but I guess that would be the most logical
xelawho is offline   Reply With Quote
Old 02-20-2013, 03:01 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
i always use .isContentEditable instead of .contentEditable, but i don't remember why it's important.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:50 PM.


Advertisement
Log in to turn off these ads.