Add two Numbers in JavaScript Inputting from KeyBoard


KeyBoard input in JavaScript:-

As like other programming languages like C, C++, Java, C# etc, JavaScript does not support direct input from console because its not working on console. JavaScript working only on web browser and the interpreter for JavaScript is browser itself.

If you want to get input from keyboard in JavaScript then JavaScript provide two options one is, input from HTML controls like TexBox and second is, prompt().

here is a tutorial for getting input by both the methods:-




Live Demo
Enter Number
Enter Number
Result

HTML Code


<tr><td>Enter Number</td><td><input id="num1" type="text" /></td></tr>
<tr><td>Enter Number</td><td><input id="num2" type="text" /></td></tr>
<tr><td>Result</td><td><input id="res" type="text" /></td></tr>
<tr><td><input onclick="sum()" type="button" value="ADD" /></td></tr>
</tbody></table>

JavaScript Code


<script type="text/javascript">
function sum()
{
var n1=parseInt(document.getElementById('num1').value);
var n2=parseInt(document.getElementById('num2').value);
document.getElementById('res').value=(n1+n2);
}
</script>

{ 0 comments... read them below or add one }

Post a Comment