To add two or more numbers in JavaScript, we can follow this approach.
<script> function Sum() { var a = document.getElementById("txtA").value; var b = document.getElementById("txtB").value; var result = parseInt(a) + parseInt(b); alert(result); } </script> Enter two digit: <input type="text" id="txtA" name="txtA" /> + <input type="text" id="txtB" name="txtA" /> <input type="button" name="btnAdd" value="Add" onclick="Sum()" />
In the above code snippet, two textboxes are there. When the button is clicked Sum function executes that gets the values of both textboxes and add it using “+” operator. At last it shows the result in the alert.
OUTPUT
Views: 5152 | Post Order: 12