Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get sum of html element's values with specific class names in the multiple divs have same class name.
HTML
<html>
<link REL="STYLESHEET" TYPE="text/css" HREF="checkoperations.css">


<script type="text/javascript" src="jquery.js"/>&lt;/script>
<script type="text/javascript" src="checkoperations.js"></script>
<body>

<input type="button" id="calculate" value="calculate"/>
<div class="testdiv">
      <input type='checkbox' name='checked2' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='7'/>
      <input type='text' class ='fieldsbankrecon' readonly='readonly' value='8'/>

</div>

<div class="testdiv"  >
      <input type='checkbox' name='checked' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='1' />
      <input type='text' class ='fieldsbankrecon2' readonly='readonly' value='2' />

</div>


<div class="testdiv"  >
      <input type='checkbox' name='checked' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='1' />
      <input type='text' class ='fieldsbankrecon2' readonly='readonly' value='2' />

</div>

<div class="testdiv"  >
      <input type='checkbox' name='checked' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='1' />
      <input type='text' class ='fieldsbankrecon2' readonly='readonly' value='2' />

</div>

<body>

</html>

jQuery code
JavaScript
var sum = 0;
$('#calculate').click(function()
    {

    $('.testdiv').each(function() {
    //var $this = $(this);
    //if ($this.is(':checked')) {
        var currentValue = parseInt($(".fieldsbankrecon1").val(),10);
        sum = sum  + currentValue;
        //}
         alert(message);

    });

Message is always 7 which is the first text box in the first div.
It's iterating four times though, but I don't know why the value is not changing.

I guess my message is not completely shown here. My HTML code has four divs with same class name that is testdiv and inside each div there are three input boxes.

I would really appreciate the help.
Posted
Updated 16-Jan-13 8:33am
v4

You would want to do something like this:
var sum = 0;        
$(".fieldsbankrecon1").each(function() 
{ 
  if(!isNaN(this.value) && this.value.length!=0) 
  {
    sum += parseFloat(this.value);            
  }         
});


Update
Add the following div to the body of your document
<div id="output">

</div>

This script will automatically recalculate the sum as the user checks and unchecks the checkboxes:
<script>
    var evaluateFields = function ()
    {

        var sum = 0;
        var n = $("input:checked").each(function ()
        {
            $(this).parent().find(".fieldsbankrecon1").each(function ()
            {
                sum += parseFloat(this.value);
            });
        });
        $("#output").text("Sum:"+sum);
    };
    evaluateFields();

    $("input[type=checkbox]").on("click", evaluateFields);

</script>


You can also add multiple fields with the appropriate class to each of your divs.

Best regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sk. Tajbir 16-Jan-13 16:01pm    
nice 5+
Espen Harlinn 16-Jan-13 16:02pm    
Thank you, Sk. Tajbir :-D
ankitrishi8584 16-Jan-13 16:46pm    
Thanks for the help Espen but what if I want to add the value of .fieldsbankrecon1 class only for the divs whose checkboxes are checked.

for example I have four divs and three of them have their checkboxes checked and I want my code pick those three divs.
ankitrishi8584 16-Jan-13 17:43pm    
Thanks I appreciate the help.
Would be thankful for the query I have
This is really so helpful and nice tutorial, i also wrote the same tutorial and i try to keep my code small and simple as possible.
https://htmlcssphptutorial.wordpress.com/2015/07/10/jquery-sum-subtract-two-input-fields/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900