4

I want to add some HTML elements that have the same class name.

So, the code will be like this with jQuery.

$(".force").each(function (){
    a += parseInt( $(this).html());
});
$("#total_forces").html(a);

In this code, the variable has to be global.

Is there any other elegant way to sum every .force value and get the sum out of the each function without global variable?

3
  • 1
    Is there a problem with the method you're showing us? Commented Dec 7, 2010 at 14:05
  • 1
    @Surreal Read, he doesn't want to use a global variable. Commented Dec 7, 2010 at 14:07
  • 1
    He states that a is a global, but I'm hoping that he will clarify if that's the problem, or it's a matter of efficiency, incorrect result, etc. Commented Dec 7, 2010 at 14:10

5 Answers 5

17

If you don't want to introduce a global variable, you could use something like this:

$("#total_forces").html(function() {
    var a = 0;
    $(".force").each(function() {
        a += parseInt($(this).html());
    });
    return a;
});
Sign up to request clarification or add additional context in comments.

3 Comments

Nice. Actually .html() also accepts a function so no need to wrap in ( ... )().
Ah, I didn't know that. Much better! Also, now I have 4 spare parentheses I can use elsewhere.
Yeah, it's important to keep balance on those suckers (Ba-dum tisssss)
4

For convenience, if you're going to be needing this same functionality frequently, I'd probably just make a plugin.

Example: https://jsfiddle.net/tzw4mkL2/

(function( $ ) {
    $.fn.sumHTML = function() {
       var sum = 0;
        this.each(function() {
           var num = parseInt( $(this).html(), 10 );
           sum += (num || 0);
        });
       return sum; 
    };
})( jQuery );

...which would be used like this:

$('#total_forces').html( $('.force').sumHTML() );

EDIT: Changed to guard agains NaN as noted by @Šime Vidas. Also ensured base-10 in the parseInt() and fixed a closing } that was missing.

1 Comment

It may make sense to guard against NaN.
2

In short, no.

Why does a have to be global? It doesn't have to be global.

function aFunc() {
    var a = 0;

    $(".force").each(function (){
        a += parseInt( $(this).html());
    });

    return a;
}

$("#total_forces").html(aFunc());

Which, can be simplified to:

$("#total_forces").html(function() {
    var a = 0;

    $(".force").each(function (){
        a += parseInt( $(this).html());
    });

    return a;
});

Here a is local to aFunc, and is just one of millions of examples of it not being in the global scope.

Comments

1

Don't want a global?

(function() {
    var a = 0;
    $('.force').each(function (){
        a += parseInt($(this).text());
    });
    $('#total_forces').text(a);
})();

Comments

0

You can use $(".force").length, it returns the number of elements in the jQuery object.

jQuery API

1 Comment

I think @Raphappa wants the sum of the values in the .force elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.