JavaScript issue to sum fields issue fields in jira,issue.fields,http://issue.fields.issuetype.

This is a basic html/javascript code, but I am having issues getting the sum of all fields. (The are 50 fields in the original project, but now I just leave 5)

If the field is blank, it just has to ignore it, and add only those with filled fields.

HTML code:

value1:<input type="text" id="total_1" ><br>
value2:<input type="text" id="total_2" ><br>
value3:<input type="text" id="total_3" ><br>
value4:<input type="text" id="total_4" ><br>
value5:<input type="text" id="total_5" ><br>
total:<input type="text" id="totalresult" >
<button type="button" onclick="getTotal(); return false;">Get total</button>

Javascript:

function getTotal() {
var sum;
for (i = 1; i <=5 ; i++) {
    var total = document.getElementById('total_' + i.toString()).value;
    if (total != '') {
        sum = parseFloat(total) + sum;
        document.getElementById('totalresult').value = sum;
    }

  }

}

I don't know why my code is isn't working.

Here is my Fiddle

Answer:1

You need to do two things. 1, initialize sum to zero. 2, check the input values for not being a number.

function getTotal() {
    var sum = 0;
    for (i = 1; i <= 5; i++) {
        var total = document.getElementById('total_' + i).value;
        if (!isNaN(parseFloat(total))) sum = parseFloat(total) + sum;
        document.getElementById('totalresult').value = sum;
    }
}

jsFiddle example

Answer:2

I don't know why your fiddle can't figure out to make getTotal a global. But your main problem is that sum is undefined as start. This will result in NaN (Not a Number) :

var sum;

sum = 1 + sum; // NaN

....

sum = 1 + undefined; // NaN

sum = 1 + NaN; // NaN

Demo at jsbin.com

You should set sum equal zero at first:

var sum = 0;
for ( ... ) { ...

Working demo as adrianp pointed out: It would probably be more clear if you uploaded the working code to jsbin.

Answer:3

This is the code when i am adding in all textbox it works fine but after removing number it shows NaN function sumVal() { var total = 0; var coll = document.getElementsByTagName("input") ...

This is the code when i am adding in all textbox it works fine but after removing number it shows NaN function sumVal() { var total = 0; var coll = document.getElementsByTagName("input") ...

I already tried to swap the functions on owl.carousel.js but it only works when the mouse moves. var Autoplay = function(scope) { this.core = scope; this.core.options = $.extend({}, Autoplay.Defaults,...

I already tried to swap the functions on owl.carousel.js but it only works when the mouse moves. var Autoplay = function(scope) { this.core = scope; this.core.options = $.extend({}, Autoplay.Defaults,...

  1. carousel candies
  2. carousel candle
  3. carousel candyland
  4. carousel candies monterey
  5. carousel candy dispenser
  6. carousel candle holder
  7. carousel candle topper
  8. carousel candy bags
  9. carousel candy machine
  10. carousel candle set
  11. carousel candy apples
  12. carousel candy store
  13. carousel candyland kahala
  14. carousel candy table
  15. carousel candyland popcorn
  16. carousel candy monterey ca
  17. carousel cannington
  18. carousel candy dish
  19. carousel canalside buffalo
  20. carousel candies geneva

I have the following Javascript code which should rapidly switch between two images: <head runat="server"> <title>Home Page</title> <script src="Resources/jQuery.js" ...

I have the following Javascript code which should rapidly switch between two images: <head runat="server"> <title>Home Page</title> <script src="Resources/jQuery.js" ...

  1. javascript switching between tabs

I am using ssl communication with websocket. so I added the secure communication in my javascript code like this "wss://myip" when lunch a websocket communication from my page with https://myip i get ...

I am using ssl communication with websocket. so I added the secure communication in my javascript code like this "wss://myip" when lunch a websocket communication from my page with https://myip i get ...

  1. error when using winscp to upload files
  2. error when using airplay
  3. error when using sourcemap for reporting an error
  4. error when using
  5. internal error when using 110 boost
  6. error occurred when using in the discovery process
  7. 404 error when using https iis
  8. math error when using quadratic formula
  9. mysqldump error when using lock tables
  10. error using horzcat
  11. error using eval matlab
  12. error using vertcat
  13. error using vlan on switch-port interface
  14. error using mex