...

View Full Version : getting a value into a textbox



Stoffel
03-03-2003, 06:23 PM
How can I get the value of "c" into the textbox result?
Can it be done with this code? If yes, what needs to be changed?
If no, it has to :D




<head>
<script language="JavaScript">
function getC(a,b,c) {
c = a * b
return c }
</script>
</head>

<body>
<form>
<input type="textbox" name="val1">
<input type="textbox" name="val2">
<input type="button" value="Calculate" onClick="getC(document.forms[0]val1.value,document.forms[0]val2.value,document.forms[0].val3.value)>
<input type="textbox" name="val3">
</form>
</body>

arnyinc
03-03-2003, 06:42 PM
Your function is close, but I re-wrote quite a bit just because of how I typically program. Not sure if it is really any different.



<html>
<head>
<script language="JavaScript">
function getC(myform) {
myform.val3.value = myform.val1.value * myform.val2.value;
}
</script>
</head>

<body>
<form>
<input type="text" name="val1">
<input type="text" name="val2">
<input type="button" value="Calculate" onClick="getC(this.form)">
<input type="text" name="val3">
</form>
</body>
</html>

Stoffel
03-03-2003, 06:46 PM
Can it also be done without the
myform.val3.value = myform.val1.value * myform.val2.value;
? So it would be something like this: c = a * b

glenngv
03-04-2003, 01:53 AM
<html>
<head>
<script language="JavaScript">
function getC(a,b) {
c = a * b;
return c;
}
</script>
</head>
<body>
<form>
<input type="text" name="val1">
<input type="text" name="val2">
<input type="button" value="Calculate" onClick="this.form.val3.value=getC(this.form.val1,this.form.val2)">
<input type="text" name="val3">
</form>
</body>
</html>

Stoffel
03-04-2003, 10:42 AM
Hmz, I get an NaN error,
(I'll fix it)
Thnx

arnyinc
03-04-2003, 01:37 PM
Just add .value to the end of both parameters when it is called in the onClick.

onClick="this.form.val3.value=getC(this.form.val1.value,this.form.val2.value)"

glenngv
03-05-2003, 02:39 AM
oh yes, i forgot that...
you may also want to check if the input is a number or not:

function getC(a,b) {
if (isNaN(a) || isNaN(b)){
alert("Please enter a valid number.");
return;
}
c = a * b;
return c;
}

Stoffel
03-05-2003, 10:48 AM
Thnx!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum