View Full Version : Text Box adding - A Simple One
miles_rich
01-29-2003, 01:18 PM
I need to have 3 text boxes, and when i enter a number in to the the first two i want the third one to add up the first two and out put the answer in it. does anyone have the code for this as i have never programmed in javascript before.
requestcode
01-29-2003, 07:41 PM
Here is one that I got from somebody else:
<html>
<head>
<title>Doing some Math</title>
<script language="JavaScript">
function focusIt() {
window.document.forms[0].elements[0].focus();
}
function calc() {
var b = parseFloat(document.calculator.b.value);
var c = parseFloat(document.calculator.c.value);
var d = b + c;
var ansr = Math.round(d * 100) / 100;
var valu = ansr.toString();
var number = valu.split(".");
if(!number[1])
{
document.calculator.ans.value=valu+".00"
}
else
{
if(number[1].length<2)
{
number[1]="."+number[1]+"0"
document.calculator.ans.value=valu+"0"
}
else
{
document.calculator.ans.value=valu
}
}
}
</script>
</head>
<body onload="focusIt();">
<form name="calculator" onreset="return focusIt();">
<input tabindex="1" type="text" name="b")"><br>
<input tabindex="2" type="text" name="c"><br>
<input tabindex="4" type="text" name="ans"><br>
<input tabindex="3" type="button" value="calculate" onClick="calc()">
<input tabindex="5" type="reset" name="reset">
</form>
</body>
</html>
king443
01-29-2003, 09:21 PM
Here is a simple one
<html>
<head>
<title>Simple add</title>
<script language="JavaScript">
function add()
{
var a = parseInt(document.calc.a.value)+0;
var b = parseInt(document.calc.b.value)+0;
var c = a + b;
document.calc.c.value=parseInt(c);
}
</script>
</head>
<body> <form name="calc">
<input type="text" name="a" OnKeyUp="add()"> + </input>
<input type="text" name="b" OnKeyUp="add()"> = </input>
<input type="text" name="c" READONLY><br> </input>
</form> </body>
</html>
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.