Friday, November 27, 2009

[jQuery] Re: Problem traversing up list

$('#item1-1').parent().closest('li').attr('id') will be faster
because it won't retrieve the full list of parents

On 27 nov, 13:20, Michel Belleville <michel.bellevi...@gmail.com>
wrote:
> Well, that must mean that the parent has no id, which is exactly the case :
>
> <ul>
>     <li id="item1">Item 1 <= $('#item1-1').parent().parent()
>       <ul> <= $('#item1-1').parent()
>         <li id="item1-1"> <= $('#item1-1')
>           Item 1 - 1
>         </li>
>         <li id="item1-2">
>           Item 1 - 2
>         </li>
>       </ul>
>     </li>
> </ul>
>
> Or you can do this in a better way :
> $('#item1-1').parents('li:first').attr('id')
>
> Michel Belleville
>
> 2009/11/27 ximo wallas <igguan...@yahoo.com>
>
> >  It returns an empty string, so it alerts, but nothing...
> > I will show you all the code:
>
> > First I get the id of the element via URL with the URL param plugin:
> > (It works, if I do an alert it returns the right stuff)
> > var target = $.url.param("target");
> > alert($("#"+target).parent().attr('id'));
>
> > This alerts nothing !!
>
> > --- On *Fri, 11/27/09, Michel Belleville <michel.bellevi...@gmail.com>*wrote:
>
> > From: Michel Belleville <michel.bellevi...@gmail.com>
> > Subject: Re: [jQuery] Problem traversing up list
> > To: jquery-en@googlegroups.com
> > Date: Friday, November 27, 2009, 11:10 AM
>
> > You don't need to use .parentNode() (vanilla DOM) but .parent() (jQuery
> > flavor).
> > Then you can access any attribute using .attr().
>
> > So instead try : $('#item1-1').parent().attr('id')
> > This should "tadaaa".
>
> > Michel Belleville
>
> > 2009/11/27 ximo wallas <igguan...@yahoo.com<http://mc/compose?to=igguan...@yahoo.com>
>
> >>  Hello there, after too much sarching I have almost give it up with the
> >> following.
> >> I have a list with another list nested:
> >> <ul>
> >>     <li id="item1">Item 1
> >>       <ul>
> >>         <li id="item1-1">
> >>           Item 1 - 1
> >>         </li>
> >>         <li id="item1-2">
> >>           Item 1 - 2
> >>         </li>
> >>       </ul>
> >>     </li>
> >> </ul>
> >> Let's say I know the ID of "item1-2" or "item1-1" how can I get the id of
> >> the li (item1)?
> >> I have tried with:
> >> alert($('#item1-1').parentNode().id)
> >> But it gaves me an error, maybe just because it is an aberration...

No comments: