JQuery and ancestors

Y

Yanick

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 ?
 
Y

Yanick

Yanick said:
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).ancestors('ul')[0] or something.
 
K

Kevin Scholl

Peter said:
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):

http://www.nabble.com/JQuery-f15494.html

--

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

======================================================
Kevin Scholl http://www.ksscholl.com/
(e-mail address removed)
 
Y

Yanick

Kevin said:
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):

http://www.nabble.com/JQuery-f15494.html

--

Thanks to you both. I found this :

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

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

Cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top