How To Add Two Numbers Using JavaScript

This is a very simple example of adding 2 numbers in JavaScript.  Here, we are displaying a total using our custom getTotal() function.

<html>
<head>
<title>JavaScript Tutorial </title>

<script type="text/javascript">
 function getTotal(){

 var first = document.getElementById('first') .value;
 var second = document.getElementById('second') .value;
 var total  = parseInt(first) + parseInt(second);

document.getElementById('total') .value = total;
}
</script>
</head>
<body>

<input  type="textbox" id="first" placeholder="Enter First Number" /> <br /><br />
<input  type="textbox" id="second" placeholder="Enter Second Number" /> <br /><br />
<input  type="textbox" id="total" placeholder="Total" /> <br /><br />
<input  type="button" onclick="getTotal()"  value="Get Total"/> 

</body>
</html>

 

  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •