Click to See Complete Forum and Search --> : Who knew Basic math with JS would be this tough?


mcemond
01-12-2004, 12:52 PM
Hi, I'm trying to figure out some (what I believe to be) basic stuff. What I want to do is have the users enter ammounts into text boxes and have it all total on the html page at the end.

Here is an example of an equation, names of textboxes are in ():

( (sites) X 150 ) + ( (units) X 24 ) + 750 = (total)

If someone could please give me a nudge in the right direction maybe I could figure out a bunch of other equations on my own. I tried messing with this for aday or so with no luck. I think I may be barking up the wrong tree :(

Thanks for your help!
Mike

TheBearMay
01-12-2004, 01:29 PM
You can get the TEXT (since you are multiplying it's not a problem though) that is in the textbox by using the .value attribute, i.e. unit.value

Thus your equation becomes:

( sites.value * 150 ) + ( units.value * 24 ) + 750 = total.value;

jaegernaut
01-12-2004, 01:34 PM
<< Okay TheBearMay answered first, but I already typed this up so here it is :D >>

I wasn't sure how you wanted to display the total, i.e. on the fly or using a calculate button.

Here is an example using an calculate button and shows the total in a text box.

You should be able to adapt this to your needs, but if you put your code on here then we might be able to be a little more specific.

In the Head


<script type="text/javascript">
<!--

function calc()
{
document.form1.T3.value = 750 + (document.form1.T1.value * 150) + (document.form1.T2.value * 24);
}
-->
</script>




In the body


<form name="form1">

<p>Sites<input type="text" name="T1" size="20"></p>
<p>Units<input type="text" name="T2" size="20"></p>

<p>Total <input type="text" name="T3" size="20" value="0"></p>

<p>
<input type="button" value="Calculate" onClick="calc()" name="B1"></p>
</form>

mcemond
01-12-2004, 02:07 PM
Thanks so much guys! It takes me a while to understand but I think I'm starting to comprehend from your examples. I would have put my code up but it was a total mess. I will try these out and get back to you.
Thanks!
Mike