Add two number of text box :- JavaScript


  • 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:- 
  1. <html> 
  2.  
  3. <head> 
  4.  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  5.  
  6.  <script> 
  7.   function showanswer() 
  8.   { 
  9.    var t1 = document.getElementById("txt1").value; 
  10.    var t2 = document.getElementById("txt2").value; 
  11.    var ans = parseInt(t1) + parseInt(t2); 
  12.  
  13.    document.getElementById("txt3").value = ans; 
  14.   } 
  15.  
  16.   function cleartxt() 
  17.   { 
  18.    document.getElementById("txt1").value = ""; 
  19.    document.getElementById("txt2").value = ""; 
  20.    document.getElementById("txt3").value = ""; 
  21.   } 
  22.  </script> 
  23.  
  24. </head> 
  25.  
  26. <body> 
  27.  
  28.  Enter First Value : 
  29.  <input type="text" id="txt1"> 
  30.  <br> 
  31.  <br> Enter Second Value : 
  32.  <input type="text" id="txt2"> 
  33.  <br> 
  34.  <br> Answer : 
  35.  <input type="text" id="txt3"> 
  36.  <br> 
  37.  <br> 
  38.  <input type="button" value="Answer" onclick="showanswer();"> 
  39.  <br> 
  40.  <br> 
  41.  <input type="button" value="Clear" onclick="cleartxt();"> 
  42.  
  43. </body> 
  44.  
  45. </html>
Add two number of text box :- JavaScript Add two number of text box :- JavaScript Reviewed by Network security on November 13, 2019 Rating: 5

No comments:

Useful Information

Powered by Blogger.