- ParseInt() is used to convert from string to integer value.It must use, because when get the value from text box, in a default data type is string.
- document.getElementById("txt1").value = "" this is empty the tex1 html element value.
- ans = parseInt(t1) + parseInt(t2) in this line variable t1 and t2 is convert to integer and add using + operator.
- Code:-
- <html>
- <head>
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <script>
- function showanswer()
- {
- var t1 = document.getElementById("txt1").value;
- var t2 = document.getElementById("txt2").value;
- var ans = parseInt(t1) + parseInt(t2);
- document.getElementById("txt3").value = ans;
- }
- function cleartxt()
- {
- document.getElementById("txt1").value = "";
- document.getElementById("txt2").value = "";
- document.getElementById("txt3").value = "";
- }
- </script>
- </head>
- <body>
- Enter First Value :
- <input type="text" id="txt1">
- <br>
- <br> Enter Second Value :
- <input type="text" id="txt2">
- <br>
- <br> Answer :
- <input type="text" id="txt3">
- <br>
- <br>
- <input type="button" value="Answer" onclick="showanswer();">
- <br>
- <br>
- <input type="button" value="Clear" onclick="cleartxt();">
- </body>
- </html>
Add two number of text box :- JavaScript
Reviewed by Network security
on
November 13, 2019
Rating:
No comments: