1

What I'm trying to achieve is this:

I have multiple td's with the same class but different values.

<td class="goals_238"> 3 </td>
<td class="goals_311"> 0 </td>
<td class="goals_128"> 1 </td>
<td class="goals_238"> 4 </td>
<td class="goals_238"> 3 </td>

I want to get the values of "goals_238" and add them up.

The end result for "goals_238" would be = 10

This whole thing is for a sports results table, hence "goals" classes.

I can get to print how many times "goals_238" appears, but doing the math is whole other level for me.

var gamesPlayed = ( $('.goals_238').length );
$('p#goals_238').html(gamesPlayed);

Any clues on how would I go about this?

1 Answer 1

3
var sum = 0;
$.each($('.goals_238'), function(i, e){
    sum += parseInt($(e).html());
});
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly! Thanks PinnyM.

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.