Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts

    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...

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    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;
    }

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    that would... I'm always a little nervous about while loops, but I guess that would be the most logical

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,401
    Thanks
    11
    Thanked 595 Times in 575 Posts
    i always use .isContentEditable instead of .contentEditable, but i don't remember why it's important.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •