html - Using a textbox to take in an integer value for use in Javascript function. Is there a simpler way? -
html - Using a textbox to take in an integer value for use in Javascript function. Is there a simpler way? -
this question has reply here:
convert string number in javascript 4 answersi'm making simple calculator utilize in intro javascript lesson. know if there's simpler way of taking value html input textbox integer having utilize javascript parse() method?
html:
<input type = "text" id="num1">
js:
var num1 = parseint(document.getelementbyid("num1").value);
you can forcefulness string 32-bit integer this:
var num1 = ~~document.getelementbyid("num1").value;
you can, alternatively, take number possible fractional part with
var num1 = +document.getelementbyid("num1").value;
in both cases, can check see if original string did number using isnan()
:
if (isnan(num1)) { // bad input }
javascript html parsing
Comments
Post a Comment