views:

31

answers:

2

Hi I have the following code

<ul id="sample-menu-1" class="sf-menu">
    <li class="current"><a href="#a">main item <span class="cntnews">0</span></a>
        <ul>
            <li><a href="#ab">sub item 1 <span class="cntnews">0</span></a>
                <ul>
                    <li><a href="#">menu item <span class="cntnews">4</span></a></li>
                    <li><a href="#aba">menu item <span class="cntnews">8</span></a></li>
                    <li><a href="#"> sub item 2<span class="cntnews">0</span></a>
                        <ul>
                            <li><a href="#">menu item <span class="cntnews">6</span></a></li>
                            <li><a href="#">menu item <span class="cntnews">8</span></a></li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>

What I would like to do in my jQuery script is have the <span class="cntnews">0</a> have the sum of these items of there descendants

so for mainitem this will be 26, for sub item 1 this will be 26, and for subitem 2 this will be 14

I would like this to work for any unordered list. So far im not getting the result I want so any help would be appreciated

+1  A: 
jQuery.fn.calcSum = function() {

    var total = 0;

    this.find('span.cntnews').each(function(){
        total += +jQuery.text([this]);
    }).eq(0).text(total);

    return total;

};

Usage:

jQuery('#sample-menu-1').calcSum(); // => 26
J-P
this works for the total but I would like to have each of the spans with cntnews = 0 to have the count of the items of there descendants
nokiko
Just edited the plugin -- it should fill the first `<span class=cntnews">...` (within any given `<ul>`) with the total.
J-P
J-P, could you explain the #total += +jQuery.text([this]);# Its fascinating! I think I know what its doing, but not sure how. and the square brackets [this]? and does $.text(..) work in other situations?
Moin Zaman
A: 

I would do it like this:

<!doctype html>
<html>
<head>
    <title></title>
    <style type="text/css">

    </style>
</head>
<body>
<ul id="sample-menu-1" class="sf-menu">
    <li class="current"><a href="#a">main item <span class="cntnews">0</span></a>
        <ul>
            <li><a href="#ab">sub item 1 <span class="cntnews">0</span></a>
                <ul>
                    <li><a href="#">menu item <span class="cntnews">4</span></a></li>
                    <li><a href="#aba">menu item <span class="cntnews">8</span></a></li>
                    <li><a href="#"> sub item 2<span class="cntnews">0</span></a>
                        <ul>
                            <li><a href="#">menu item <span class="cntnews">6</span></a></li>
                            <li><a href="#">menu item <span class="cntnews">8</span></a></li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
    $(function() {
        $('span.cntnews').each(function(i){
            var total = 0;
            $(this).parent().parent().find('span.cntnews').each(function(j){
                total += +jQuery.text([this]); //credit to J-P for this
            });
            $(this).text(total);
        });
    })
</script>
</body>
</html>
Moin Zaman
great this works I also see what i did wrong in my early attempt
nokiko