3

I found [this][1], rather difficult, javascript example online and I've implemented it with success in my website.

However, I would like to get the result of, in this case, the two subtotals in one new text-field.

The traditional getElementbyId and total.value=total didn't work out.

EDIT

 function doMath()
{
    // Capture the entered values of two input boxes
    var twogiga = document.getElementById('twogig').value;
    var fourgiga = document.getElementById('fourgig').value;

    // Add them together and display
    var sum = parseInt(twogiga) + parseInt(fourgiga);
    document.getElementById('total').value = parseInt(sum);
}

This is the javascript I use. But for some reason, when I have just one value (twogig), the total is set as NaN. What is wrong with my script?

8
  • try to build some html first so you and us get a better idea Commented Jun 24, 2011 at 2:52
  • ok, no problem. I can give the HTML to you in a sec Commented Jun 24, 2011 at 2:54
  • Not clear with the questions, some more details required to answer ur query Commented Jun 24, 2011 at 2:58
  • you have a lost link on this in your first line. Commented Jun 24, 2011 at 3:00
  • 1
    Not the problem with your script, but if you've already used parseInt on all of the numbers you're going to be adding together you don't need to use it again on the sum. Commented Jun 27, 2011 at 0:06

3 Answers 3

7

Well, if you have assigned ID's to text inputs, for example: <input type="text" id="my_input" />, then you can call it with document.getElementById('my_input').value.

So:

<input type="text" id="my_input1" />
<input type="text" id="my_input2" />
<input type="button" value="Add Them Together" onclick="doMath();" />

<script type="text/javascript">
    function doMath()
    {
        // Capture the entered values of two input boxes
        var my_input1 = document.getElementById('my_input1').value;
        var my_input2 = document.getElementById('my_input2').value;

        // Add them together and display
        var sum = parseInt(my_input1) + parseInt(my_input2);
        document.write(sum);
    }
</script>

Naturally, that is a very basic script, it doesn't check to make sure the entered values are numbers. We have to convert the fields into integers, otherwise they'll be strings (so 2+2 would equal 22). When the button is clicked, the function is called, which makes a variable for each input box, converts them to ints, adds them, and outputs our sum.

Sign up to request clarification or add additional context in comments.

12 Comments

You should be defining a radix for the parseInt function, otherwise your results will be messed up.
Well, that's completely optional, and unless you want to use a hex or octa, it will default to decimal. Of course, if you do want to use a radex, use: parseInt(my_input1, 10); (10 means decimal). Personally, I don't really use them at all, and it would only mess up if the user tries to enter a number that starts with a 0 or an "x".
Ok, that's helpful! How do I get the value sum in a defined textfield? myText.value = sum?
Make an input field, give it an id, for example my_sum, and then use: document.getElementById('my_sum').value = sum;
Ok, seems like a good solution. But whenever I enter just 1 textfield, I get an NaN error. So both fields are required to make the sum?
|
3

Replace:

document.getElementById('total').value = parseInt(sum);

With:

document.getElementById('total').value = sum;

You have already done all the int-parsing you need to do.

Comments

0

$("#value1, #value2").on('focusout', function() {
  var value2 = parseInt($("#value2").val()) > 0 ? parseInt($("#value2").val()) : 0;
  var value1 = parseInt($("#value1").val()) > 0 ? parseInt($("#value1").val()) : 0
  var sumOfValues = value1 + value2;
  console.log('Your sum is ' + sumOfValues);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" class="input" id="value1">
<input type="text" class="input" id="value2">

Comments

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.