Solved

JS some simple additions/subtractions

Posted on 2016-09-19
8
34 Views
Last Modified: 2016-09-21
Hi there, I currently have a JS that works great, just need a couple of items added to it

<table>
<tr>
	<td align=center><input type="checkbox" name="transID" value="672"></td>
	<td><a href=?account=2&transaction=672>22/04/2016</a></td>
	<td>SureSave Account</td>
  <td>Comm return</td>
  <td align=right colspan=2><input type='text' size=10 name='debitamount' id="debitamount672" value="$63.00"></td>
</tr>
<tr>
	<td align=center><input type="checkbox" name="transID" value="618"></td>
	<td><a href=?account=2&transaction=618>28/04/2016</a></td>
	<td>Air Tickets Account</td>
  <td>&nbsp;</td>
  <td align=left colspan=2><input type='text' size=10 name='debitamount' id="creditamount618" value="$1,471.24"></td>
</tr>
<tr>
	<td align=center><input type="checkbox" name="transID" value="701"></td>
	<td><a href=?account=2&transaction=701>29/04/2016</a></td>
	<td>SureSave Account</td>
  <td>Comm return</td>
  <td align=right colspan=2><input type='text' size=10 name='debitamount' id="debitamount701" value="$45.60"></td>
</tr>

<tr>
	<td colspan=4>TOTALS</td>
	<td align=right><input type="text" name="debittotal" id="debittotal" size=10 disabled></td>
	<td align=right><input type="text" name="credittotal" id="credittotal" size=10 disabled></td>
</tr>
<tr>
	<td>Recon Amount</td>
	<td align=right><input type="text" name="reconamount" id="recomamount" size=10></td>
	<td colspan=2 align="right">Recon Amount</td>
	<td align=right><input type="text" name="totalrecon" id="totalrecon" size=10 disabled></td>
	<td align=right><input type="submit" name="submitrecon"></td>
</tr>
</table>

Open in new window

JS
$(document).ready( function() {

  $('input:checkbox').change(function () {
      var sum = 0;
      var val = 0;
      var sum2 = 0;
      var val2 = 0;
      $(':input:checked').each(function() {          
        if( $(this).is(':checked') ) {
        	if( $("#debitamount" + $(this).val()).length ) {
          		val2 = $("#debitamount" + $(this).val()).val().replace("$", "").replace(",", "");
          		sum2 += parseFloat( val2 );
          }
          if( $("#creditamount" + $(this).val()).length ) {
		          val = $("#creditamount" + $(this).val()).val().replace("$", "").replace(",", "");
          		sum += parseFloat( val );
          }
        }
      });
      $("#debittotal").val( '$' + formatNumWithComma( parseFloat( sum2 ).toFixed(2) ) );
      $("#credittotal").val( '$' + formatNumWithComma( parseFloat( sum ).toFixed(2) ) );
  });
  
  function formatNumWithComma(x) {
    return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}

});

Open in new window


The functions I would like to add are:
1) totalrecon = the total value of debittotal and credit total
2) the subtraction of reconamount from totalrecon

Thanks all! :)
0
Comment
Question by:Graeme
  • 4
  • 4
8 Comments
 
LVL 42

Assisted Solution

by:zephyr_hex
zephyr_hex earned 500 total points
ID: 41805096
It's not clear to me where you want the difference to go, so you'll need to replace "myId" in the code below with the id of the element that should receive the value.

  $('input:checkbox').change(function () {
      var sum = 0;
      var val = 0;
      var sum2 = 0;
      var val2 = 0;
      $(':input:checked').each(function() {          
        if( $(this).is(':checked') ) {
        	if( $("#debitamount" + $(this).val()).length ) {
          		val2 = $("#debitamount" + $(this).val()).val().replace("$", "").replace(",", "");
          		sum2 += parseFloat( val2 );
          }
          if( $("#creditamount" + $(this).val()).length ) {
		          val = $("#creditamount" + $(this).val()).val().replace("$", "").replace(",", "");
          		sum += parseFloat( val );
          }
        }
      });
	  sum2 = parseFloat( sum2 ).toFixed(2);
	  sum = parseFloat( sum ).toFixed(2);
      $("#debittotal").val( '$' + formatNumWithComma(sum2));
      $("#credittotal").val( '$' + formatNumWithComma(sum));
	  var totalrecon = sum2 + sum;
	  $('#totalrecon').val(formatNumWithComma(totalrecon));
	  var diff = totalrecon - parseFloat($('#recomamount').val());
	  //not sure where the difference goes
	  $('#myId').val(formatNumWithComma(diff));
  });

Open in new window

0
 
LVL 42

Expert Comment

by:zephyr_hex
ID: 41805107
As a side note, you really should do some validation on the values being received from the inputs.  Much of this code depends on the inputs having valid values.  You can't always guarantee the user will input valid values.
0
 

Author Comment

by:Graeme
ID: 41805127
Sorry I missed out 1 box for Diff

When I total the, when I added some figures (in the real sheet) for some reason, it added a whole stack of numbers after it, eg

4,281.179,999,999,999
where it should be
4,281.18

I know all the figures in the db are only upto 2 decimal place (and all the items being added in are calculated into 2 decimal place as well), so its not the data... ideas?
0
 

Author Comment

by:Graeme
ID: 41805142
ignore the last, i forgot 2 lines of your code which fixed that

now the issue is...

totalrecon, when it sums, instead of summing (credittotal and debittotal), it put the values side-by-side, eg 494-100 = 494100, where should be 394
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 42

Accepted Solution

by:
zephyr_hex earned 500 total points
ID: 41805220
Ah, I see the problem.  You need move up the lines that handle the totals.  This is because the function that handles the comma formatting changes the original value to a string instead of assigning it to a new variable.

sum2 = parseFloat( sum2 ).toFixed(2);
sum = parseFloat( sum ).toFixed(2);
var totalrecon = sum2 + sum;
var diff = totalrecon - parseFloat($('#recomamount').val());
$("#debittotal").val( '$' + formatNumWithComma(sum2));
$("#credittotal").val( '$' + formatNumWithComma(sum));
$('#totalrecon').val( '$' + formatNumWithComma(totalrecon));
//not sure where the difference goes
$('#myId').val('$' + formatNumWithComma(diff));

Open in new window

0
 

Author Comment

by:Graeme
ID: 41808238
Thanks for that fix

Yes the difference goes here:

<tr>
	<td>Recon Amount</td>
	<td align=left><input type="text" name="reconamount" id="reconamount" size="10"></td>
>	<td align=left><input type="text" name="recondiff" id="recondiff" size="10" disabled></td>
	<td align="right">Total Recon Amount</td>
	<td align=right><input type="text" name="totalrecon" size="10" id="totalrecon" disabled></td>
	<td align=right><input type="submit" name="submitrecon" value="Recon"></td>
</tr>

Open in new window

0
 
LVL 42

Assisted Solution

by:zephyr_hex
zephyr_hex earned 500 total points
ID: 41808998
so this line:
//not sure where the difference goes
$('#myId').val('$' + formatNumWithComma(diff));

Open in new window


would be:
$('#recondiff').val('$' + formatNumWithComma(diff));

Open in new window

0
 

Author Closing Comment

by:Graeme
ID: 41809001
Cheers mate, works a treat! :)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now