- Add Two Numbers in a WebPage
- Add Two Numbers using Form and TexBox
- Receive input from user one by one
Save this code in a file with .html extension. Open the file in a web browser. Here is the output you will see:
Note - The document.write() method writes the value passed in its braces (), into an HTML output.
Here is its sample run with user input, 40 as first number and 50 as second number:
When user clicks on the button, Add, then a function named add() gets called. And all the statements of this function gets executed. The following statement:
states that, an int (integer) value of an HTML element whose id is first gets initialized to numOne. And the statement:
states that, the value of sum gets printed to an HTML element with id, answer.
Live Output of Previous Program
Get User Input one by one
This program receives input from user in a way that, user is allowed to input one at a time.
Now enter the first number say 50 and click on Enter button. Here is the output after performing this:
Now enter the second number say 40 and click on Add button. Here is the output that shows the addition result of entered two numbers:
The following code:
states that, an HTML element whose id is stored in temp variable, gets visible after executing this statement. And the following statement:
states that, the content of an HTML element with id res gets replaced with the value of sum.