JQuery and ancestors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yanick

    JQuery and ancestors

    Is it possible, with JQuery to get only one specific element ancestor ?
    For example :

    <div>
    <ul id="tree">
    <li><a>Item 1</a>
    <ul id="subTree">
    <li><a>Item 1.1</a></li>
    </ul>
    </li>
    </ul>
    </div>


    $('ul li a').click( function() {

    // get first UL here

    } );

    That is, if I click on "item 1" I want to get the "tree" DOMelement,
    and when I click on "item 1.1" I want to get the "subTree" DOMelement
    only. How can this be achieved ?

  • Yanick

    #2
    Re: JQuery and ancestors


    Yanick wrote:
    Is it possible, with JQuery to get only one specific element ancestor ?
    For example :
    >
    <div>
    <ul id="tree">
    <li><a>Item 1</a>
    <ul id="subTree">
    <li><a>Item 1.1</a></li>
    </ul>
    </li>
    </ul>
    </div>
    >
    >
    $('ul li a').click( function() {
    >
    // get first UL here
    >
    } );
    >
    That is, if I click on "item 1" I want to get the "tree" DOMelement,
    and when I click on "item 1.1" I want to get the "subTree" DOMelement
    only. How can this be achieved ?
    I should specify : I would rather like to avoid
    $(this).parent( ).parent() or similar syntax, and would prefer use
    something like $(this).ancesto rs('ul')[0] or something.

    Comment

    • Peter Michaux

      #3
      Re: JQuery and ancestors

      Yanick wrote:
      Is it possible, with JQuery to get only one specific element ancestor ?

      If no one here has an answer you will probably have better luck with
      the jquery mailing list

      <URL: http://jquery.com/discuss/>

      Peter

      Comment

      • Kevin Scholl

        #4
        Re: JQuery and ancestors

        Peter Michaux wrote:
        Yanick wrote:
        >Is it possible, with JQuery to get only one specific element ancestor ?
        >
        >
        If no one here has an answer you will probably have better luck with
        the jquery mailing list
        >
        <URL: http://jquery.com/discuss/>
        I would suggest that the luck would be better there in any circumstance.
        The JQuery community is extremely helpful, and generally very quick to
        respond.

        Web-based access to the JQuery list (via Nabble):



        --

        *** Remove the DELETE from my address to reply ***

        =============== =============== =============== =========
        Kevin Scholl http://www.ksscholl.com/
        kscholl@comcast .DELETE.net
        ------------------------------------------------------
        Information Architecture, Web Design and Development
        ------------------------------------------------------
        We are the music makers, and we are the dreamers of
        the dreams...
        =============== =============== =============== =========

        Comment

        • Yanick

          #5
          Re: JQuery and ancestors


          Kevin Scholl wrote:
          Peter Michaux wrote:
          Yanick wrote:
          Is it possible, with JQuery to get only one specific element ancestor ?

          If no one here has an answer you will probably have better luck with
          the jquery mailing list

          <URL: http://jquery.com/discuss/>
          >
          I would suggest that the luck would be better there in any circumstance.
          The JQuery community is extremely helpful, and generally very quick to
          respond.
          >
          Web-based access to the JQuery list (via Nabble):
          >

          >
          --
          Thanks to you both. I found this :

          $(this).parents ('ul:first')

          I guess it's the most simple way to get this.

          Cheers.
          >
          *** Remove the DELETE from my address to reply ***
          >
          =============== =============== =============== =========
          Kevin Scholl http://www.ksscholl.com/
          kscholl@comcast .DELETE.net
          ------------------------------------------------------
          Information Architecture, Web Design and Development
          ------------------------------------------------------
          We are the music makers, and we are the dreamers of
          the dreams...
          =============== =============== =============== =========

          Comment

          Working...