0

1)I want to add the values of two input boxes and show it in the third input. It shows an error

2)Error: Uncaught TypeError: Cannot read property 'value' of null at sum (js.php:21) at HTMLButtonElement.onclick (js.php:12)

<!DOCTYPE html>
<html>

  <head>
    <title>JavaScript</title>
    <script type="text/javascript">
      function sum() {
        var val1 = document.getElementById('val1').value;
        var val2 = document.getElementById('val2').value;
        var sum = val1 + val1;
        document.getElementById('total').value = sum;
      }

    </script>
  </head>

  <body>
    <input type="text" name="val1"><br><br>
    <input type="text" name="val2"><br><br>
    <input disabled name="total" value=""><br><br>
    <button type="button" onclick="sum()" name="btn">Add</button>
  </body>

</html>

I expected the sum of two inputs but it shows nothing.

3
  • correct it like this <input type="text" name="val1" id="val1"><br><br> Commented May 7, 2019 at 6:33
  • now, you have all the answers below :) Commented May 7, 2019 at 6:36
  • check this code stackoverflow.com/a/16668731/5326667 Commented May 7, 2019 at 6:44

7 Answers 7

0

You should have the attribute id for the controls which you are referencing in your code with getElementById(). You also have to convert the string value to number to perform the arithmetic operation:

function sum(){
  var val1 = document.getElementById('val1').value;
  var val2 = document.getElementById('val2').value;
  var sum = Number(val1) + Number(val2);
  document.getElementById('total').value = sum;
}
<input type="text" name="val1" id="val1"><br><br>
<input type="text" name="val2" id="val2"><br><br>
<input name="total" id="total" value=""><br><br>
<button type="button" onclick="sum()" name="btn">Add</button>

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

1 Comment

It works. I forgot to Add IDs To HTML Elements, Thanks.
0

You are using HTML DOM getElementById() Method, so you should assign an Id to your input total in order to resolve the issue:

<input type="hidden" name="total" id="total" value="">

Comments

0

You should add Id to your input. You use getElementById, but your input doesn't have any id, so it will retourn null.

<input type="text" name="val1" id="val1"><br><br>
<input type="text" name="val2" id="id="val2"><br><br>
<input type="hidden"  name="total" value="" id="total"><br><br>

1 Comment

Thanks For Explanation, I Forget To Add IDs
0

You can do with click on button to sum of two input value OR with use of onblur events also

                <script type="text/javascript">
                function sum(){
                  var val_1 = document.getElementById('val1').value;
                  var val_2 = document.getElementById('val2').value;
                  var sum = Number(val_1) + Number(val_2);
                  document.getElementById('total').value = sum;
                }
                </script>

                <input type="text" id="val1" onblur="sum()"><br><br>
                <input type="text" id="val2" onblur="sum()"><br><br>
                <input id="total" value=""><br><br>
                <button type="button" onclick="sum()" name="btn">Add</button>

2 Comments

It works. I forgot to Add IDs To HTML Elements, Thanks and iam also searching for onblur event thanks for extra information
@samad Yes definitely help onblur events, and Your Welcome...
0

You should use id attribute to get the input value which you are referencing in your code with getElementById().

and You also have to convert the string value to number to perform the arithmetic operation because when we get value from inputs it will a string value

for convert string to number you can use + sign like in code

<!DOCTYPE html>
<html>
  <head>
    <title>JavaScript</title>     
    <script type="text/javascript">
    function sum(){
      var val1 = +document.getElementById('val1').value;
      var val2 = +document.getElementById('val2').value;
      document.getElementById('total').value = val1+val2;
    }     
  </script>
  </head>
  <body>
  <input type="text" name="val1" id="val1"><br><br>
  <input type="text" name="val2" id="val2"><br><br>
    <input type="hidden"  name="total" id="total" value=""><br><br>
    <button type="button" onclick="sum()" name="btn">Add</button>

  </body>
</html>

Comments

0

document.getElementsByName returns a NodeList of elements so it doesn't have property .value.

This is getElementsByName snippet.

<!DOCTYPE html>
<html>

<head>
  <title>JavaScript</title>
  <script type="text/javascript">
    function sum() {

      var val1 = document.getElementsByName('val1')[0].value;
      var val2 = document.getElementsByName('val2')[0].value;
      var sum = parseInt(val1) + parseInt(val2); // by default it appends a string. Hence, parseInt to parse string to integer

      document.getElementsByName('total')[0].value = sum;
      console.log(document.getElementsByName('total')[0].value);
    }
  </script>
</head>

<body>
  <input type="text" name="val1"><br><br>
  <input type="text" name="val2"><br><br>
  <input type="hidden" name="total" value=""><br><br>
  <button type="button" onclick="sum()" name="btn">Add</button>
</body>

</html>

This is getElementById snippet

<!DOCTYPE html>
<html>

<head>
  <title>JavaScript</title>
  <script type="text/javascript">
    function sum() {
      var val1 = document.getElementById('val1').value;
      var val2 = document.getElementById('val2').value;
      var sum = parseInt(val1) + parseInt(val1); // by default it appends a string. Hence, parseInt to parse string to integer

      document.getElementById('total').value = sum;
      console.log(document.getElementById('total').value);
    }
  </script>
</head>

<body>
  <input type="text" name="val1" id="val1"><br><br>
  <input type="text" name="val2" id="val2"><br><br>
  <input type="hidden" name="total" value="" id="total"><br><br>
  <button type="button" onclick="sum()" name="btn">Add</button>
</body>

</html>

1 Comment

I get a lot of confusions but you clear my conclusions by comparing getElementsByName and getElementById , Thanks
0

Try This

<script>
 function sum(){
        var val1 = document.getElementById('val1').value;
        var val2 = document.getElementById('val2').value;
        var sum = +val1 + +val2;
       document.getElementById('total').value = sum;
      }  
</script>

<body>
    <input type="text" name="val1" id="val1"><br><br>
    <input type="text" name="val2" id="val2"><br><br>
     <input type="hidden"  name="total" id="total" value=""><br><br>
     <button type="button" onclick="sum()" name="btn">Add</button>
   </body>

OR Fetch through name

<script>
 function sum(){
        var val1 = document.getElementsByName('val1')[0].value;
        var val2 = document.getElementsByName('val2')[0].value;
        var sum = +val1 + +val2;
       document.getElementsByName('total')[0].value = sum;
      }  
</script>

<body>
    <input type="text" name="val1" ><br><br>
    <input type="text" name="val2" ><br><br>
     <input type="hidden"  name="total" value=""><br><br>
     <button type="button" onclick="sum()" name="btn">Add</button>
   </body>

3 Comments

@KrishnaPrashatt : Please Have look.
It works. I forgot to Add IDs To HTML Elements, Thanks.
This shouldn't be the accepted answer IMHO. Please refer How to Answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.