Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate text boxes using the onChange event

Status
Not open for further replies.

celley

Programmer
Jul 31, 2000
54
A2
I would like to have some sort of script that will allow me to do the following:<br><br>Have 10 text boxes, allow the user to enter numbers and then have them calculated in an 11th text box before the form is submitted.<br><br>I know that i need to use the onChange event.....<br><br>I have a working page that uses drop-down menu's, but I want to use text boxes.<br><br>Thanks,<br>Chad<br><A HREF="mailto:celley@gobcg.com">celley@gobcg.com</A><br>
 
you're able to do this with drop down menus and NOT with text boxes ?????!!!!!! it should be the same, except that <br>you have to use code like that to get the values from the drop downs : &quot;my_list.options[my_list.selectedIndex]. ....&quot; and for the textboxes it's only my_textbox.value ...<br>maybe i didn't get your question ??
 
so could I do the following?&nbsp;&nbsp;I dont know enough javascript to do it.... call me dumb.<br><br>&lt;script type=&quot;text/javascript&quot;&gt;<br>function updatesum() {<br>&nbsp;document.form.sum.value =<br>(document.form.bcg1.value[document.form.bcg1.value].text-0) +<br>(document.form.bcg2.value[document.form.bcg2.value].text-0);}<br>&lt;/script&gt;<br><br><br><br>&lt;form name=&quot;form&quot; action=&quot;form2.cfm&quot; method=&quot;post&quot;&gt;<br><br>&lt;input type=&quot;text&quot; name=&quot;bcg1&quot; onChange=&quot;updatesum()&quot; &gt;<br>&lt;input type=&quot;text&quot; name=&quot;bcg2&quot; onChange=&quot;updatesum()&quot; &gt;<br><br><br>&lt;br&gt;<br>&lt;input name=&quot;sum&quot; readonly value=&quot;(not computed)&quot; size=15&gt;<br><br>&lt;/form&gt;
 
ur not dumb, just inexpirenced.<br><br>try this:<i><br>function updatesum(){<br>document.form.sum.value=<br>(document.form.bcg1.value+document.form.bcg2.value);<br>}</i><br><br>with the rest the same<br> <p>theEclipse<br><a href=mailto:eclipse_web@hotmail.com>eclipse_web@hotmail.com</a><br><a href=robacarp.webjump.com>robacarp.webjump.com</a><br>**-Trying to build a documentation of a Javascript DOM, crossbrowser, of course. E-mail me if you know of any little known events and/or methods, etc.
 
I did what you had wrote and it adds the two as strings.&nbsp;&nbsp;I want it to add the numbers for a sum.<br><br>How would I verify that they were numeric as well?
 
I looked on those pages but didnt find anything....
 
it actually IS in the docs but hey, here's the code ;]<br><br>function updatesum() {<br>&nbsp;&nbsp;var first_one = parseInt(document.form.bcg1.value, 10)<br>// first_one is now either an integer in radix 10, OR it's &quot;isNaN&quot; that is a special value saying it's NOT a number<br>&nbsp;&nbsp;var second_one = parseInt(document.form.bcg2.value, 10)<br>// same for second_one<br>// btw you can use ParseFloat instead of parseInt depending on what you actually want <br><br>// ok now some checking <br>&nbsp;&nbsp;if (isNaN(first_one)) { // it's NOT a number !<br>&nbsp;&nbsp;&nbsp;&nbsp;document.form.sum.value=&quot;the 1st value has to b an integer !!&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;return false<br>&nbsp;&nbsp;}<br>&nbsp;&nbsp;if (isNaN(second_one)) {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... same ...<br>&nbsp;&nbsp;}<br><br>// ok now everything's fine let's set the sum field to the correct value<br>&nbsp;&nbsp;document.form.sum.value = first_one + second_one;<br>}<br><br>if this doesn't work try to replace the last line with :<br>&nbsp;var tmp= eval(&quot;first_one + second_one&quot;);<br>&nbsp;document.form.sum.value = tmp;<br>but you shouldn't have to i think
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top