Get sum of all DOM nodes having same class with JavaScript or jQuery

Shasha

My question is simple let's say you have multiple <span> elements like this

 <span class="hi">10,000</span>
 <span class="hi">20,000</span>
 <span class="hi">40,000</span>
 <span class="hi">500,000</span>

How do you get each number content inside the span tag, strip the commas away and add the numbers get the answer and add commas, cause I can't seem to figure out a way to do this.

This is what I want to achieve

//data would be gotten from Span tag with class HI

<script> 
var addthevalue = 10000+20000+40000+500000;
$(#theans).html(addthevalue);
</script>

 <div id=theans></div>
Cat
var total = 0;
$(".hi").each(function() {
    total += parseInt($(this).html().replace(",", ""));
});
$("#theans").html(total);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

javascript, can't change class on all elements of the same class

分類Dev

Javascript get element index position in DOM array by class or id

分類Dev

Nokogiri get all HTML nodes

分類Dev

Remove rows having same value in all columns

分類Dev

How to fill form fields having same class

分類Dev

JQuery get selected/single value of data with same class

分類Dev

Javascript regex: get all numbers having between 2 and 6 digits but not 5

分類Dev

C# Get all nodes in xml document but ignoring nested nodes

分類Dev

jquery - select an element if children not having a specific class

分類Dev

Get all the keys for a class

分類Dev

javascript Sum two class elements result to class

分類Dev

Angular2 - Get component light DOM innerHTML / child nodes

分類Dev

jQuery validation not working separately on each class. It is working on all element at the same time

分類Dev

Jquery being applied to all divs with same class but needs to do each indivudally

分類Dev

Setting object attributes in a loop results in all attributes having the same value

分類Dev

Is it possile to cast two different class but having the same attributes?

分類Dev

Get Matched Class in Jquery

分類Dev

Get SUM of query result WITHIN same query

分類Dev

Using jQuery in javascript class

分類Dev

Get the index of elements with same class

分類Dev

How to get sum of all max column value

分類Dev

Javascript: sum all found .data() for an array of items

分類Dev

Javascript: each element of a array of objects having same value

分類Dev

Javascript how do sum class numeric value?

分類Dev

jQuery: selecting an element having a certain id and a certain class, both as variables

分類Dev

Using JavaScript and jQuery in same file

分類Dev

Get index of a DOM object within a jQuery object

分類Dev

allow access to all resources on kubernetes cluster except get nodes

分類Dev

Get xpath of all nodes in XML tree with attributes - Python

Related 関連記事

  1. 1

    javascript, can't change class on all elements of the same class

  2. 2

    Javascript get element index position in DOM array by class or id

  3. 3

    Nokogiri get all HTML nodes

  4. 4

    Remove rows having same value in all columns

  5. 5

    How to fill form fields having same class

  6. 6

    JQuery get selected/single value of data with same class

  7. 7

    Javascript regex: get all numbers having between 2 and 6 digits but not 5

  8. 8

    C# Get all nodes in xml document but ignoring nested nodes

  9. 9

    jquery - select an element if children not having a specific class

  10. 10

    Get all the keys for a class

  11. 11

    javascript Sum two class elements result to class

  12. 12

    Angular2 - Get component light DOM innerHTML / child nodes

  13. 13

    jQuery validation not working separately on each class. It is working on all element at the same time

  14. 14

    Jquery being applied to all divs with same class but needs to do each indivudally

  15. 15

    Setting object attributes in a loop results in all attributes having the same value

  16. 16

    Is it possile to cast two different class but having the same attributes?

  17. 17

    Get Matched Class in Jquery

  18. 18

    Get SUM of query result WITHIN same query

  19. 19

    Using jQuery in javascript class

  20. 20

    Get the index of elements with same class

  21. 21

    How to get sum of all max column value

  22. 22

    Javascript: sum all found .data() for an array of items

  23. 23

    Javascript: each element of a array of objects having same value

  24. 24

    Javascript how do sum class numeric value?

  25. 25

    jQuery: selecting an element having a certain id and a certain class, both as variables

  26. 26

    Using JavaScript and jQuery in same file

  27. 27

    Get index of a DOM object within a jQuery object

  28. 28

    allow access to all resources on kubernetes cluster except get nodes

  29. 29

    Get xpath of all nodes in XML tree with attributes - Python

ホットタグ

アーカイブ