214-564-5316
Friday, May 16, 2008
•
Portfolio
•
FAQ's
•
Pricing
•
Request Info
•
Code Examples
•
Demos
•
Marketing
•
Tutorials
•
Downloads
•
Add numbers using javascript
Add two numbers with javascript:
First Number:
Second Number:
Here is the code:
<script language="JAVASCRIPT"> <!-- function addTWD() { document.TWDform.Num3.value = parseInt(document.TWDform.Num1.value) + parseInt(document.TWDform.Num2.value); } //--> </script> <form name="TWDform"> <table> <tr> <td colspan="2" align="CENTER"><b>Add two numbers with Javascript</b></td> </tr> <tr> <td>First Number:</td> <td><input type="TEXT" name="Num1" value=""></td> </tr> <tr> <td>Second Number:</td> <td><input type="TEXT" name="Num2" value=""></td> </tr> <tr> <td> </td> <td> <input type="BUTTON" name="AddButton" value="Add the numbers" onclick="JavaScript:addTWD(); true;"> <input type="RESET"></td> </tr> <tr> <td>Total:</td> <td><input type="TEXT" name="Num3" value=""></td> </tr> </table> </form>
OR
Add a column of numbers with javascript
(we've labeled them for ease of identification in the code):
H1
T1
H2
T2
H3
T3
T4
Here is the code:
<script type="text/javascript" language="javascript"> function getTotal(oForm) { var field, i = 0, total = 0, els = oForm.elements; var fieldnames = ['H1' , 'T1' , 'H2' , 'T2' , 'H3' , 'T3' , 'T4']; for (i; i < fieldnames.length; ++i) { field = els[fieldnames[i]]; if (field.value != '' && isNaN(field.value)) { alert('Please enter a valid number here.') field.focus(); field.select(); return ''; } else total += Number(field.value); } return ' ' + total; } </script> </head> <body> <form> <input id="H1" type="text" name="H1" value="" />___H1<br /> <input id="T1" type="text" name="T1" value="" />___T1<br /> <input id="H2" type="text" name="H2" value="" />___H2<br /> <input id="T2" type="text" name="T2" value="" />___T2<br /> <input id="H3" type="text" name="H3" value="" />___H3<br /> <input id="T3" type="text" name="T3" value="" />___T3<br /> <input id="T4" type="text" name="T4" value="" />___T4 <br /><br /> <input id="total" type="text" name="total" value="" readonly="readonly" /> <input type="button" value="Get Total" onclick="total.value=getTotal(this.form)" /> <input type="reset" /> </form>
•
Home
•
Contact Us
•
Terms of Service
•
Privacy Policy
•
Email Policy
•
Hosting
•
Links
•
Site Search
•