Get Integer from HTML text to JavaScript - Stack Overflow

I have a text field like this:<input id="num" type="text" style="width:10px

I have a text field like this:

<input id="num" type="text" style="width:10px"/>

I am trying to retrieve it from JavaScript as follows:

var numofnodes =parseInt(document.getElementById('num').value,10);

It is giving "", and i tried like this

var numofnodes =document.getElementById('num').value

But still I am getting "".

I have a text field like this:

<input id="num" type="text" style="width:10px"/>

I am trying to retrieve it from JavaScript as follows:

var numofnodes =parseInt(document.getElementById('num').value,10);

It is giving "", and i tried like this

var numofnodes =document.getElementById('num').value

But still I am getting "".

Share Improve this question edited Aug 2, 2015 at 1:27 Nathan Tuggy 2,24427 gold badges32 silver badges38 bronze badges asked Aug 2, 2015 at 1:21 user5049245user5049245 4
  • What is the value in the field? What were you expecting? – Scott Hunter Commented Aug 2, 2015 at 1:23
  • I am giving a number say 100 – user5049245 Commented Aug 2, 2015 at 1:28
  • Any chance that the document has more than one element with the id="num" attribute? Any errors in the console? – Ted Hopp Commented Aug 2, 2015 at 1:49
  • It does not contain any element with id=num, I am using visual studio so i saw by using a break point and found that the numofnodes="" – user5049245 Commented Aug 2, 2015 at 1:51
Add a ment  | 

4 Answers 4

Reset to default 3

Problem seems that you are trying to read the value on page load, i.e. textbox isn't having any value given, hence you get blank:

var numofnodes = parseInt(document.getElementById('num').value, 10);
alert(numofnodes);
<input id="num" type="text" style="width:10px" />

Try with value attribute for default no. of nodes:

var numofnodes = parseInt(document.getElementById('num').value, 10);
alert(numofnodes);
<input id="num" type="text" style="width:10px" value="100" />

parseInt either returns an integer or NaN, it shouldn't be a "". How do you output the value that you get? Your code seems to works fine for me:

<input id="num"  type="text" style="width:100px"/>
<button id="b">log value</button>

...

document.getElementById("b").onclick = function()  {
  console.log( parseInt(document.getElementById('num').value, 10) );
}

http://codepen.io/anon/pen/LVMzxr?editors=101

I tried your code, and for me it works correctly.

<input id="num" type="text" style="width:10px;" value=100 />

added value into attribute

$(document).ready(function() {
    // print into console as int
    var numofnodes =parseInt(document.getElementById('num').value,10);
    console.log(numofnodes);

    // print into console as string
    var numofnodes =document.getElementById('num').value 
    console.log(numofnodes);
});

Hmm, surprisingly enough, no one mentioned the event's built-in property for this.

event.target.valueAsNumber

will give the value as a number for inputs with type number without any conversions.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744302944a4567588.html

相关推荐

  • Get Integer from HTML text to JavaScript - Stack Overflow

    I have a text field like this:<input id="num" type="text" style="width:10px

    7天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信