views:

50

answers:

2

Hello, I have a structure like this:

<ul id="mycustomid">
    <li><a>Item A</a>
        <ul class="children">
           <li><a>Child1 of A</a>
               <ul class="children">
                 <li><a>Grandchild of A</a>
                    <ul class="children">
                       <li><a>Grand Grand child of A</a></li>
                    </ul>
                </li>
               </ul>
           </li>
        </ul>
    </li>
    <li><a>Item B</a></li>
    <li><a>Item C</a></li>
</ul>

Now, I am using Jquery to get only the immediate children of the ul#mycustomid. But my code returns me with ALL the li's in my structure. How should I proceed?

Here's my code:

$(document).ready(function() {
 var arri = $("#mycustomid>li").text();
 alert(arri);
});

I have tried .children() too, it gives me almost the same result. This is really getting on my nerves :(

My alert box outputs exactly as shown below (including those white gaps):

Item A 
Child1 of A
Grandchild of A

Grand Grandchild of A


ItemBItemC

Whereas, it should be just (without spaces):

Item A 
Item B
Item C

To understand my problem, you can check out the live version at http://jsfiddle.net/yS6ZJ/

Please point me in the right direction,

Thanks

+5  A: 

I think that your selector is working just fine - it's what you're doing with it that's got a problem.

When you call .text(), you get all the contents of the element. All of it, including the child elements.

Try this:

$('#mycustomid > li').each(function() {
  alert($(this).find('a:eq(0)').text());
});
Pointy
+1 Exactly what I was about to say.
MvanGeest
Thank you, it works awesome, can you please tell me *how* that logic works? I'm amazed by your intelligence (I feel like a stupid noob, ofcourse)
imaginonic
Just keep in mind that something like ".text()" or ".html()" applies to the complete contents of an element. What my sample does is loop through the immediate child `<li>` elements (just like yours), and for each one it finds the **first** `<a>` tag (that's what the ":eq(0)" trick does) and grabs the content of that. Because it's *just* getting the text of that one `<a>` tag, it doesn't pick up the whole "tree" in the `<li>`
Pointy
Thank you Pointy, thank you very much. You saved me loads n loads of pain and headache! By the way, just curious, how do I access the children (and grandchildren) using your logic ?
imaginonic
Well, from each `<li>` you could start up *another* `.find()` to do a similar search. Alternatively, you could take an entirely different approach: just find *all* the `<li>` elements, and then for each one use the jQuery `parentsUntil("#mycustomid")` to get a list of parent elements up to that root node. By the length of the list of parents, you'll be able to tell how "deep" each `<li>` is.
Pointy
Thank you, thank you. Just can't thank you enough, Pointy!
imaginonic
A: 

This will solve your issue:

$(document).ready(function() {
  var arri=$("#mycustomid > li").children('a').append('\n').text();
  alert(arri);
});

The append('\n') is there just to add a line break, so that it looks okay in the alert.

To include all grandchildren, you just remove the immediate reference to LI:

$(document).ready(function() {
  var arri=$("#mycustomid li").children('a').append('\n').text();
  alert(arri);
});

And to just get the grandchildren:

$(document).ready(function() {
  var arri=$("#mycustomid li").not("#mycustomid > li").children('a').append('\n').text();
  alert(arri);
});

And to get each level, you can go for:

$(document).ready(function() {
  var ChildOfA=$("#mycustomid > li > ul > li > a").append('\n').text();
  var GrandchildOfA=$("#mycustomid > li > ul > li > ul > li > a").append('\n').text();
  var GrandGrandChildOfA=$("#mycustomid > li > ul > li > ul > li > ul > li > a").append('\n').text();
  alert('List of Child1 of A children:\n\n'+ChildOfA+'\n\n'+
        'List of Grandchild of A children:\n\n'+GrandchildOfA+'\n\n'+
        'List of Grand Grand child of A children:\n\n'+GrandGrandChildOfA);
});
Gert G
Wow!Never knew it'd be soo simple :D Thanks Gert :) You rock! (and, yes, you too Pointy!!) Thank you all =) Peace, yo.
imaginonic
Gert, I don't want to 'include' the Grandchildren, instead, I want to access them or rather, display just the Grand children, just like you've done with the top level items in your first example..Can you help?
imaginonic
No problem. Just exclude the direct LIs. See my addition above.
Gert G
Thank you, but it doesn't seem to work, Gert :( It includes the Children, Grand children and Grand,Grand children.I just need to access a single level of elements - like either only the Grandchildren series or only the Grand Grand Children seriesFiddle - http://jsfiddle.net/EDvwj/
imaginonic
See above. Hopefully this is what you're looking for.
Gert G
Perhaps this is what they refer to as "Pure Awesomeness". Thanks a million Gert, you made my day, saved me from a lot of pain. Bless you, and thanks a lot!!
imaginonic
No problem. It worked out in the end. :) Cheers!
Gert G
Bless you Gert :)
imaginonic