Wednesday, December 5, 2012

Add Two Numbers in JavaScript

Below function addNumbers() will parse the entered numbers in the textboxes. Add them together and display the result in textfield on the button click event.

    <title>Input tutorial</title>
    <script language="javascript">
     function addNumbers()
       var val1 = parseInt(document.getElementById("value1").value);
       var val2 = parseInt(document.getElementById("value2").value);
       var ansD = document.getElementById("answer");
       ansD.value = val1 + val2;
  value1 = <input type="text" id="value1" name="value1" value="1"/>
  value2 = <input type="text" id="value2" name="value2" value="2"/>
  <input type="button" name="Sumbit" value="Click here" onclick="javascript:addNumbers()"/>
  Answer = <input type="text" id="answer" name="answer" value=""/>

No comments:
Write comments
Recommended Posts × +