1

My HTML "sum.html" file seems not work with my "mathbox.js" file. I have declare it in the HTML head, however, when checking in Chrome console the num1 and num2 seems to work along with the sum by putting it sum = num1 + num2.

This is my Javascript function;

var num1 = Number(num1);
var num1 = Number(num1);
function calSum(){

    var num1 = document.getElementById("num1").innerHTML;
    var num2 = document.getElementById("num2").innerHTML;
    var add = num1 + num2;
    document.getElementById("sum").inneHTML = add;
}

Below is my HTML

<!DOCTYPE HTML PUBLIC>

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Matchbox</title>
<script type ="text/javascript" src="mathbox.js"></script>
</head>
<body>

<div>




<table border="1">
<tr>
<td style="text-align:right">First number:</td>
<td>
   <input type="text" id = "num1" />
</td>
<td rowspan="2">
   <input type="button" value="Calculate" onclick="calSum()">
</td>
</tr>

<tr>
<td style="text-align:right">Second number:</td>
<td>
   <input type="text" id ="num2" />
</td>
</tr>
</table>

<br>
<table border="1">
<tr>
<td style="text-align:right">Sum:</td>
<td style="width:100px"> <input type="text" id ="sum" onclick="cal" /></td>
</tr>
</table>

</div>
</body></html>
1
  • Try document.getElementById("sum").value = add; Commented Feb 28, 2014 at 4:06

7 Answers 7

2
var num1 = Number(num1);  //called only once and it will suffer error

You can't parse it as global variable because each time you have to parse the string values as number and to fetch the value of textbox use value attribute.

function calSum(){
   var num1 = +document.getElementById("num1").value;
           // |_________used to parse the string as number type
   var num2 = +document.getElementById("num2").value;
           // |_________used to parse the string as number type
   var add = num1 + num2;
    document.getElementById("sum").value= add;
}
Sign up to request clarification or add additional context in comments.

7 Comments

So you're saying to remove global num1 and num2 outside of the function?
@user2985145 it is not within function, I'm saying there is no use in having var num1 = Number(num1); var num1 = Number(num1); these lines.
Thank you everyone. It now works. You're guys are awesom
wow! +1 ... is it the safe way to parse the string to number with unary operator +?
|
1

document.getElementById("num1") returns a string. You need to convert that to a number before adding using Number(). Otherwise it will do string concatenation. Also instead of using .innerHTML you have to use .value.

function calSum(){

    var num1 = document.getElementById("num1").innerHTML;
    var num2 = document.getElementById("num2").innerHTML;
    var add = Number(num1) + Number(num2);
    document.getElementById("sum").value = add;
}

Comments

1

You can not use .innerHTML for input type text

use as below :

document.getElementById("sum").value = add;

Replace YOur Function as below:

function calSum(){

    var num1 = document.getElementById("num1").value;
    var num2 = document.getElementById("num2").value;
    var add = parseFloat(num1) + parseFloat(num2);

    //or you can use  var add =  Number(num1) + Number(num2);
    document.getElementById("sum").value= add;
}

5 Comments

parseInt may not always work without giving the base. as second parameter
@SajithNair - The second parameter is optional. If omitted it defaults to a radix of 10. If that is what the OP wants is not clear, but it will work.
@Steve This is not correct. Please go and check what is parseInt("0.2") and Number("0.2"). Please make sure you are not misleading the person who is asking doubt.
@SajithNair - You stated "parseInt may not always work without giving the base. as second parameter". That is a wrong statement. It ALWAYS works without a second parameter. Your example uses parseInt with a string representation of a float. Of course that doesn't work... but that has nothing to do with the second parameter.
Thanks Steve & sajith for point me my mistake. i have updated my answer
0

Use this

function calSum(){
    var num1 = document.getElementById("num1").value;
    var num2 = document.getElementById("num2").value;
    var add =  Number(num1) + Number(num2);
    document.getElementById("sum").value = add;
}

In your code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">

function calSum(){
    alert('sasa');
    var num1 = document.getElementById("num1").value;
    var num2 = document.getElementById("num2").value;
    var add =  Number(num1) + Number(num2);
    document.getElementById("sum").value = add;
}
</script>
<title>Untitled Document</title>
</head>
<body>

<table border="1">
<tr>
<td style="text-align:right">First number:</td>
<td>
   <input type="text" id="num1" />
</td>
<td rowspan="2">
   <input type="button" value="Calculate" onclick="calSum()">
</td>
</tr>

<tr>
<td style="text-align:right">Second number:</td>
<td>
   <input type="text" id="num2" />
</td>
</tr>
</table>
<br>
<table border="1">
<tr>
<td style="text-align:right">Sum:</td>
<td style="width:100px"> <input type="text" id ="sum" onclick="cal" /></td>
</tr>
</table>
</body>
</html>

Comments

0
<input type="text" id ="sum" onclick="cal" />

input element doesn't have innerHTML property. You have to use value property in your Javascript.

Just like this ...

document.getElementById("sum").value = add;

Comments

0

For the input box we are using value not innerHTML , innerHTML is used for the containers like div,td etc..

so please change your function

function calSum(){
   var num1 = +document.getElementById("num1").value;
   var num2 = +document.getElementById("num2").value;
   var add = num1 + num2;
    document.getElementById("sum").value= add;
}

1 Comment

As others have pointed out, this would do string concatenation. You probably want to make sure num1 and num2 are numbers, not strings.
0

Change your function as:

function calSum()
{
    var num1 = document.getElementById("num1").value;   
    var num2 = document.getElementById("num2").value;   

    if(!isNaN(num1) && !isNaN(num2))
    {
        add = parseFloat(num1) + parseFloat(num2);
    }   

    document.getElementById("sum").value = add;     
}

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.