I am trying to write a simple form on HTML/Javascript where I type in a number, adds one and then displays it. I got it working but it was concatenating the one instead of adding it. I used the parseInt(), but it is showing "Nan". Any suggestions?
The HTML is:
<head> <link src="script.js" type="text/javascript"></script></head>
<body>
<input type="text" id="grossSalary">
<button type="button" onclick="calculateTax()">
Generate Tax
</button>
</body>
The Javascript is:
function calculateTax(){
var salaryString = document.getElementById("grossSalary");
var salaryInteger = parseInt(salaryString);
alert(salaryInteger + 1);
}
The "error" message I get is:
"Nan"
I am trying to write a simple form on HTML/Javascript where I type in a number, adds one and then displays it. I got it working but it was concatenating the one instead of adding it. I used the parseInt(), but it is showing "Nan". Any suggestions?
The HTML is:
<head> <link src="script.js" type="text/javascript"></script></head>
<body>
<input type="text" id="grossSalary">
<button type="button" onclick="calculateTax()">
Generate Tax
</button>
</body>
The Javascript is:
function calculateTax(){
var salaryString = document.getElementById("grossSalary");
var salaryInteger = parseInt(salaryString);
alert(salaryInteger + 1);
}
The "error" message I get is:
"Nan"
Share Improve this question asked Jan 23, 2015 at 6:15 GilGil 5352 gold badges10 silver badges24 bronze badges 2-
An additional note to the existing answers. You should always define the
radix
for parseInt,parseInt(salaryString, 10);
, otherwise you might get an unexpected result when someone decides to add a leading0
to the salary. – t.niese Commented Jan 23, 2015 at 6:23 - Oh geez. This is my first stack overflow question and boy you guys are fast and geniuses! – Gil Commented Jan 23, 2015 at 6:33
6 Answers
Reset to default 3You forgot to extract value from input element:
var salaryString = document.getElementById("grossSalary").value;
Add .value
var salaryString = document.getElementById("grossSalary").value;
You need to get the value, which is missed in your code.
var salaryString = document.getElementById("grossSalary").value;
The problem is in your javascript where you set the salaryString variable. You need to make sure to collect the value of the element you are selecting. Like so:
var salaryString = document.getElementById("grossSalary").value;
After you do that, your code should behave as expected.
The getElementById() method returns the element that has the ID attribute with the specified value. So when you try like
var salaryString = document.getElementById("grossSalary");
so here salaryString is a reference to an Element object, or null if an element with the specified ID is not in the document.
If you are looking fro get the value of the corresponding text box then use something like
var salaryString = document.getElementById("grossSalary").value;
To find value from textbox you have to use .value. so update your JS code
function calculateTax(){
var sal = document.getElementById("grossSalary");
var salaryInteger = parseInt(sal.value);
alert(salaryInteger + 1);
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745096662a4611019.html
评论列表(0条)