Basic Calculation

  • Thread starter Thread starter 116
  • Start date Start date
1

116

New to scripting in Frontpage. I would like to add a Adv. button, and on
click increment the value of a field by any given number. Little html
knowledge, some VB.

Thanks
David
 
116 said:
New to scripting in Frontpage. I would like to add a Adv. button, and on
click increment the value of a field by any given number. Little html
knowledge, some VB.

Thanks
David

Well, try JavaScript

<html>
<head>
<title>Advance a Number</title>
</head>
<script type="text/javascript">
function calcform()
{
with (document.form1)
{ number1.value = +number1.value + +number2.value}
/* Note the extra + unary operators. */
}
</script>
<body>
<form name="form1" action="">
Number:<br />
<input type="text" value="10" id="number1" size="10" /><br/>
Enter Number to advance by:<br />
<input type="text" value="1" id="number2" size="10" /><br/>
<input type="button" id="Advance" value="Advance"
onclick="calcform()" >
<input type="reset" id="reset" value=" Reset "/>
</form>
</body>
</html>
 
Thank you. Works well. Something I didn't consider, was the fact that I can
have a single digit (1, 2, 3, etc.). Once I get into 2 digits, sorting is no
longer valid. Who can I format the field to contain a leading zero on single
digits?

Thanks
David
 
Do you mean that the result (the number after "Number1:") can be less than
10.

If so, change the function to convert the result to add a leading zero.
function calcform()
{
with (document.form1)
{ number1.value = +number1.value + +number2.value;
if (number1.value < 10)
number1.value = '0' + number1.value;
}
/* Note the extra + operators. */
}

But if it gets greater than 100, you will need two leading zeroes when < 10
and one leading zero when between 10 and 99 inclusive
function calcform()
{
with (document.form1)
{ number1.value = +number1.value + +number2.value;
if (number1.value < 10)
number1.value = '00' + number1.value;
else if (number1.value < 100)
number1.value = '0' + number1.value;
}
/* Note the extra + operators. */
}
 
Thanks Trevor...greatly appreciate it.

Trevor Lawrence said:
Do you mean that the result (the number after "Number1:") can be less than
10.

If so, change the function to convert the result to add a leading zero.
function calcform()
{
with (document.form1)
{ number1.value = +number1.value + +number2.value;
if (number1.value < 10)
number1.value = '0' + number1.value;
}
/* Note the extra + operators. */
}

But if it gets greater than 100, you will need two leading zeroes when < 10
and one leading zero when between 10 and 99 inclusive
function calcform()
{
with (document.form1)
{ number1.value = +number1.value + +number2.value;
if (number1.value < 10)
number1.value = '00' + number1.value;
else if (number1.value < 100)
number1.value = '0' + number1.value;
}
/* Note the extra + operators. */
}
 
Back
Top