Traversing un-ordered list and appending attibute values to the list item I have written this jQuery code to traverse the HTML markup. Basically, I am selecting data-icount attribute of each anchor tag and appending it to anchor text as (28) e.g. the ALL becomes ALL (38) in the example. 38 here is the sum of all the data-icounts under that li. Similarly, summers become summers (2) because it has two data-icount 0 and 2 so 0+2 =2.

Working example here.

I am sure there must be an improved process to do this - any comments/suggestions would be appreciated.

&lt;script&gt;
    $(document).ready(function(){
        var $lis = $("#tree li");   
        $lis.each(function(index) {         
            var $this = $(this);
            var $sub_uls = $this.children('ul');
            var $child_a = $this.children('a');
            var num_sub_uls = $sub_uls.length;

            var count_icount = 0;
            if(num_sub_uls &gt; 0) {
                //var sub_lis = $this.children('a');
                var sub_lis = $sub_uls.find('a');
                //console.log('Total anchors: '+sub_lis.length);
                sub_lis.each(function(index2) {
                    count_icount += parseInt($(this).attr('data-icount'));
                    //console.log(count_icount);
                });
                //console.log("out of loop");
            } else {
                count_icount = parseInt($child_a.attr('data-icount'));
            }

            var a_txt = $child_a.text();
            $child_a.text(a_txt+' ('+count_icount+') ');
        });
    });
&lt;/script&gt;


HTML

&lt;ul id="tree" class="tree"&gt;
    &lt;li class=""&gt;
        &lt;a data-icount="0" href="/some/thing/1"&gt;ALL &lt;/a&gt;
        &lt;ul &gt;
            &lt;li class=""&gt;
                &lt;a data-icount="7" href="/some/thing/2" &gt;Fruits&lt;/a&gt;
                &lt;ul &gt;
                    &lt;li class=""&gt;
                        &lt;a data-icount="5" href="/some/thing/4" &gt;Summers&lt;/a&gt;
                        &lt;ul &gt;
                            &lt;li class=""&gt;&lt;a data-icount="0" href="/some/thing/7"&gt;Mango&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=""&gt;&lt;a data-icount="2" href="/some/thing/8" &gt;Water melon&lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/li&gt;
                    &lt;li class=""&gt;
                        &lt;a data-icount="0" href="/some/thing/5" &gt;Winters&lt;/a&gt;
                        &lt;ul &gt;
                            &lt;li class=""&gt;&lt;a data-icount="0" href="/some/thing/9" &gt;Tangerines&lt;/a&gt;&lt;/li&gt;
                            &lt;li class=""&gt;&lt;a data-icount="0" href="/some/thing/10" &gt;Pomegranate&lt;/a&gt;&lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/li&gt;
                    &lt;li class=""&gt;&lt;a data-icount="1" href="/some/thing/12" &gt;Others&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li class="open"&gt;
                &lt;a data-icount="0" href="/some/thing/3" class="selected"&gt;Furniture&lt;/a&gt;
                &lt;ul &gt;
                    &lt;li class=""&gt;&lt;a data-icount="6" href="/some/thing/6" &gt;Home&lt;/a&gt;&lt;/li&gt;
                    &lt;li class=""&gt;&lt;a data-icount="0" href="/some/thing/11" &gt;Garden&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li class=""&gt;&lt;a data-icount="10" href="/some/thing/13" &gt;Games&lt;/a&gt;
                &lt;ul &gt;
                    &lt;li class=""&gt;&lt;a data-icount="5" href="/some/thing/36" &gt;Playstation&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/li&gt;
            &lt;li class=""&gt;&lt;a data-icount="2" href="/some/thing/37" &gt;Clothes&lt;/a&gt;&lt;/li&gt;
        &lt;/ul&gt;
    &lt;/li&gt;
&lt;/ul&gt;
Here are some tips:

1) Don't create variables if they're only used once or are a don't perform complex operations.

Old Code:

var num_sub_uls = $sub_uls.length;
var count_icount = 0;
if(num_sub_uls &gt; 0) {


New Code:

var count_icount = 0;
if($sub_uls.length) {


2) Provide the base when using parseInt().

Old Code:

count_icount = parseInt($child_a.attr('data-icount'));


New Code:

count_icount = parseInt($child_a.attr('data-icount'), 10);


3) Eliminate commented code sections.

Old code:

var sub_lis = $sub_uls.find('a');
//console.log('Total anchors: '+sub_lis.length);
sub_lis.each(function(index2) {


New Code:

var sub_lis = $sub_uls.find('a');
sub_lis.each(function(index2) {


4) Simplify if conditions by using Truthy &amp; Falsey .

Boolean(undefined); // =&gt; false
Boolean(null); // =&gt; false
Boolean(false); // =&gt; false
Boolean(0); // =&gt; false
Boolean(""); // =&gt; false
Boolean(NaN); // =&gt; false

Boolean(1); // =&gt; true
Boolean([1,2,3]); // =&gt; true
Boolean(function(){}); // =&gt; true


More here    

Old Code:

if(num_sub_uls &gt; 0) {


New Code:

if(num_sub_uls) {


5) Use .append() to append text to an element in jQuery.

Old Code:

var a_txt = $child_a.text();
$child_a.text(a_txt+' ('+count_icount+') ');


New Code:

$child_a.children('a').append( ' (' + count_icount + ') ' );


6) Simplify your if and else conditions.

Check if count_icount is greater than 0, instead of checking for sub_list.length;

Old Code:

if(num_sub_uls &gt; 0) {
    var sub_lis = $sub_uls.find('a');
    sub_lis.each(function(index2) {
        count_icount += parseInt($(this).attr('data-icount'));
    });
} else {
    count_icount = parseInt($child_a.attr('data-icount'));
}


New Code:

$sub_uls.find('a').each(function(index2) {
    count_icount += parseInt($(this).attr('data-icount'));
});
if( !count_icount ){
    count_icount = parseInt($this.children('a').attr('data-icount'), 10);
}


7) Use .data() instead of .attr() to access properties that begin with data-.

.data() Documentation

Example:

&lt;a id="thing2" data-icount="9" href="/some/thing/2" &gt;Fruits&lt;/a&gt;


Use 

$("#thing2").data( "icount" ) == "9";


Instead of  

$("#thing2").attr( "data-icount" ) == "9";


Final code:

$(function () {
    $("#tree li").each(function () {
        var $this = $(this),
        count_icount = $this.find("a").data('icount');

        $this.children('ul').find('a').each(function () {
            count_icount += parseInt($(this).data('icount'), 10);
        });
        if (!count_icount) {
            count_icount = parseInt($this.children('a').data('icount'), 10);
        }
        $this.children('a').append(' (' + count_icount + ') ');
    });
});


Demo: http://jsfiddle.net/GQkB8/2/