PDA

View Full Version : Math


Dfw
02-08-2003, 09:11 PM
Is there any way to do math on a html form? If so how would I add 2 numbers that were picked and make them display and save them to my form email?

l3vi
02-08-2003, 09:38 PM
I know how to add 2 numbers that were picked, but not save them. To add them, use this code:

<script>
function add()
{
var no = document.ns.n1.value
var nt = document.ns.n2.value
var altogether = eval(no+"+"+nt)
var answer = altogether
document.body.innerHTML+=('<font color="Green"><center>The sum of your numbers is '+answer+'!</center></font>')
}
</script>
<center>
<form name="ns">
#1: <input type="text" name="n1"><br>
+
<br>
#2: <input type="text" name="n2"><br>
<input type="submit" onClick="add()" value="Add!">
</form>
</center>

for division, change the + in altogether to a slash or something, and for multlipication, change it to an asterisk. Hope that was what you were looking for!

Mhtml
02-09-2003, 01:45 PM
innerHtml isn't as compatible as innerText.
I'd recommend having..

<p id="answerSpace"></p>
<script>
function add()
{
var no = document.ns.n1.value
var nt = document.ns.n2.value
var altogether = eval(no+"+"+nt)
var answer = altogether
document.answerSpace.innerText = answer
}
</script>
<center>
<form name="ns">
#1: <input type="text" name="n1"><br>
+
<br>
#2: <input type="text" name="n2"><br>
<input type="submit" onClick="add()" value="Add!">
</form>
</center>

l3vi
02-14-2003, 03:46 AM
WOw, ya taught me sumthin! Thanks MHTML! :)

glenngv
02-14-2003, 05:47 AM
innerHTML works in IE/NS6/Mozilla, while innerText only works in IE