javascript - getElementById or parseInt not working? - Stack Overflow

I am trying to write a simple form on HTMLJavascript where I type in a number, adds one and then displ

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 leading 0 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
Add a ment  | 

6 Answers 6

Reset to default 3

You 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

相关推荐

  • javascript - getElementById or parseInt not working? - Stack Overflow

    I am trying to write a simple form on HTMLJavascript where I type in a number, adds one and then displ

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信