All Text Entries Are Strings

Another feature of form data to remember is that it treats all entries as strings. For example, in setting up an online boutique, you might want to perform math on data entered by the user. The following little script attempts to add two numbers:

<html> 
<head> 
<title> Text Box Math </title> 
<script language="JavaScript"> 
function addEmUp( ){
      var sum=document.calc.aOne.value + document.calc.aTwo.value; 
      alert(sum); 
      } 
</script> 
</head> 
<body bgcolor="#ffffaa"> 
<h4>Enter 2 numbers and press the Sum button.</h4> 
<form name="calc"> 
<input name="aOne" type=text ><P> 
<input name="aTwo" type=text ><P> 
<input type=button value="Sum" onClick="addEmUp( )"> 
</form> 
</body> 
</html> 

When you run the script, you will see, as ...

Get JavaScript Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.