Forums

This topic is locked

Simple Question

Posted 06 Dec 2002 22:12:33
1
has voted
06 Dec 2002 22:12:33 Danny Goor posted:
My question is a simple one..


I have a textfield

and I have 2 buttons.

button(A) makes the value of the textfield = 5

"So far simple enough"

button(B) multiplies the value of the textfield by 2

code : textfield.value = textfield.value * 2

"This works"

OK>!

I want to make button(B) instead of multiplying the value by 2, I want it to add 2 to the value

I.E if textfield.value = 5 , when I press button(B) I want it to = 7

my code is : textfield.value = textfield.value + 2

!!! This doesn't work., instead of performing the mathematical function it simply adds 2 to the string giving me instead of " 7 " it gives me "52"

How can I get this to work.,


Thanks.,

Sincerely., Danny

Replies

Replied 11 Dec 2002 03:07:27
11 Dec 2002 03:07:27 Dan The Man replied:
there is an extension called "form calculator" by "KaosWeaver" that will do this.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title></title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?")>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function KW_getVal(o){
var retVal="0";if (o.type=="select-one"
{retVal=(o.selectedIndex==-1)?0<img src=../images/dmxzone/forum/icon_smile_shock.gif border=0 align=middle>.options[o.selectedIndex].value;}
else if (o.length&gt;1){for (var i=0;i&lt;o.length;i++) if (o[i].checked) retVal=o[i].value;
} else if (o.type=="checkbox" {retVal=(o.checked)?o.value:0;} else {
retVal=Number(o.value)}return parseFloat(retVal);
}

function KW_calcForm() {
var str="",a=KW_calcForm.arguments; for (var i=2;i&lt;a.length;i++)
str+=(a[i].indexOf("#"==-1)?a[i]:KW_getVal(MM_findObj(a[i].substring(1)));
MM_findObj(a[0]).value=Math.round(a[1]*eval(str))/a[1];
}
//--&gt;
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action="" method="get" name="myForm"&gt;
&lt;input name="myNum" type="text" value=""&gt;&lt;br&gt;
&lt;input name="" type="button" value="Add 2" onClick="KW_calcForm('myNum',1,'#myNum','+','2')"&gt;
&lt;input name="" type="button" value="Set to 5" onClick="myNum.value=5"&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Reply to this topic