0

i have this function to calculate the sum of 3 inputs :

function calculateTotal() {

 var result = fd.field('_x002D_ESO').value() + fd.field('Artdesk').value() + fd.field('OtherDesign').value();

    // put the result somewhere into your form
	
	document.getElementsByClassName("myOutput")[0].innerHTML = "Le budget total est : " + result;

}

 

fd.field('_x002D_ESO').change(calculateTotal);

fd.field('Artdesk').change(calculateTotal);

fd.field('OtherDesign').change(calculateTotal);

calculateTotal();
        });

example . if i put 1 in the first field and 2 in the second and 3 in the third field , i dont get the sum, what i get is 123 any solution?

2
  • Can you run this piece on JSFiddle? Commented Jul 6, 2016 at 2:28
  • I just created a basic fiddle of what you may want using jquery and javascript: jsfiddle.net/chengsieuly/rzrhgxzs/1 Something like this should work. Please let me know if you have any questions. Commented Jul 6, 2016 at 2:42

1 Answer 1

1

fd.field('_x002D_ESO').value() is returning a string, you need to cast it to an integer. If the string is empty or otherwise falsy, we then set the value to be 0.

var value1 = parseInt(fd.field('_x002D_ESO').value()) || 0;
var value2 = parseInt(fd.field('Artdesk').value()) || 0;
var value3 = parseInt(fd.field('OtherDesign').value()) || 0;

var result = value1 + value2 + value3;
Sign up to request clarification or add additional context in comments.

7 Comments

i try it and still doesn t work, i get the same result
can you console.log them? What's the error? There might be some whitespace causing parseInt to fail
this is what i put : function calculateTotal() { var result = parseInt(fd.field('_x002D_ESO').value()) + parseInt(fd.field('Artdesk').value()) + parseInt(fd.field('OtherDesign').value()); // put the result somewhere into your form document.getElementsByClassName("myOutput")[0].innerHTML = "Le budget total est : " + result; } fd.field('_x002D_ESO').change(calculateTotal); fd.field('Artdesk').change(calculateTotal); fd.field('OtherDesign').change(calculateTotal); calculateTotal(); });
i dont have console error but i get the same result : if i put 1 in te frst field and 2 in the second i get 12
that's very strange, could you put a console.log(result) after the var result line?
|

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.