JavaScript string to integer

By using parseInt(str_num) function you are able to convert a string representing a number to an integer value. In the example below, the string value entered in txtval1 text box and string value entered in txtval2 text box are converted to integer values before they are added together.
<html>
<head>
<title>Calculation involving convert string to int</title>
<script type="text/javascript" language="JavaScript">
function cal(){
var val1=parseInt(document.getElementById("txtval1").value ) ;
var val2=parseInt(document.getElementById("txtval2").value ) ;
var result=val1+val2;
document.getElementById("divres"). innerHTML="Result: "+ result;
}

</script>

</head>
<body>

<form action="#">
<table>
<tr>
<td>Enter value:</td><td> <input type="text" id="txtval1" ></td>
</tr>
<tr>
<td>Enter another value: </td><td><input type="text" id="txtval2" ></td>
</tr>
<tr>
<td><input type="button" name="submit" value="Sum" onclick="cal()"></td>
</tr>
</table>
<div id="divres" style="color: fuchsia"></div>
</form>

</body>
</html>

Posted by: Dara | post date: 03-13-2013 | Subject: JavaScript




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.