Loop through a UL

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?
tonelm54Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
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
tonelm54Author 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
Rainer JeschorCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.