We help IT Professionals succeed at work.

Loop through a UL

tonelm54
tonelm54 asked
on
Im trying to do is write a tree plugin for jQuery as a bit of an exercise but running short on documentation.

What Im trying to do is go through all the first li items and if I find a ul  as a child than recursive go through, however my code just seems to be caught in a loop constantly calling the first ul.

HTML:-
<div id="testTree">
    <ul>
        <li>File</li>
        <li>Edit</li>
        <li>Help
            <ul>
                <li>Topics</li>
                <li>Search</li>
                <li>About</li>
            </ul>
        </li>
    </ul>
</div>

Open in new window


JQuery Code:-
(function ($) {
    'user strict';
    $.fn.makeTree = function () {
        DrawBranch($("ul"));
    };

    function DrawBranch($ulBranch) {
        $("ul:first() li").each(function (index) {
            console.log($(this).text());
            if ($(this).find('ul').length > 0) {
                console.log("children found");
                DrawBranch($(this).find('ul'));
            }
        });
    }
})(jQuery);

$(document).ready(function () {
    $('#testTree').makeTree();
});

Open in new window


What I believe it should be showing in alert boxes is:-
File
Edit
Help
Children Found
Topics
Search
About

However what is happening is its just looping:-
File
Edit
Help
Topics
Search
About
children found
File
Edit
Help
Topics
Search
About
children found
File
Edit
Help
Topics
Search
About
children found
File
Edit
Help
Topics
Search
About
children found
File
Edit
Help
Topics
Search
About
children found
.....etc

Im a bit confused, cause as far as Im concered it should be working ok, so a bit stuck before moving onto drawing the tree parts

Does anyone have any suggestions on looping through unordered lists?
Comment
Watch Question

Most Valuable Expert 2013
Awarded 2012

Commented:
Hi,
I readjusted the selectors.
Initially you have to select only the first level children for each passed UL.
(function ($) {
    'user strict';
    $.fn.makeTree = function () {
        DrawBranch($("ul:first"));
    };

    function DrawBranch($ulBranch) {
        $($ulBranch).children("li").each(function (index) {
            //console.log($(this).text());
            $("#result").append("<li>" + $(this).text() + "</li>");
            if ($("ul",this).length > 0) {
                //console.log("children found");
                $("#result").append("<li>Children found</li>");
                DrawBranch($("ul",this));
            }
        });
    }
})(jQuery);

$(document).ready(function () {
    $('#testTree').makeTree();
});

Open in new window


Live sample:
https://jsfiddle.net/EE_RainerJ/af30jyc9/

HTH
Rainer

Author

Commented:
Is it possible for it not to show:-
Help
Topics
Search
About

Instead just
Help

And then on the recursive call show:-
Topics
Search
About

I belive its just editing the line
$("#result").append("<li>" + $(this).text() + "</li>");

Open in new window


However after quickly playing with it I dont know how to pull just this elements text out, not the child <UL> <LI> items. I dont want to put them in a <a href> link as Ive seen before, as want to pull the data out of data attributes
Most Valuable Expert 2013
Awarded 2012
Commented:
Hi,
and here we go:
"simply" adjust the .text() with the following:
$(this).contents().filter(function (){ return this.nodeType == 3})[0].nodeValue

Open in new window


Sample:
https://jsfiddle.net/EE_RainerJ/ds1reo1t/

BUT if your original HTML source looks differently perhaps there is the need to adjust it - depending on if there are other HTML tags inside (spans, divs ...)

HTH
Rainer