PDA

Click to See Complete Forum and Search --> : Javascript: passing parameters [Resolved]


ober0330
Dec 22nd, 2003, 07:21 AM
I'm trying to pass a parameter to a function and then having the function output to the textbox that called it.

Here is my calling function:

echo "<tr><td><p class=other>" . $row[0] . "</td>";
echo "<td><p class=other><input type=text name=HourEntry" . $i ." size=10 onchange="Changehandler(i)"></td>";
echo "<td><p class=other4>" . $row2[$i + 3] . "</td>";
echo "<td><p class=other2><input type=text name=HourEntry2" . $i ." size=10></td></tr>";

And here is my function:

<script language="JavaScript 1.2" type="text/javascript">
<!--
Function ChangeHandler(i)
{
document.dform.HourEntry2.value=(parseFloat(HourEntry" + i + ".value)
- parseFloat(HourEntry2" + i + ".value));
}
//-->
</script>

I'm not sure if the function works, because I can't get the parser to get past the "Changehandler(i)" line when I call it. How do I pass the number of the textbox to the function??

And in case you need to understand what is going on, the echo lines are in a loop creating approx. 30 text boxes and I need to have the user type text into the first one and have it update the next one.

ober0330
Dec 22nd, 2003, 07:38 AM
ok... new calling line:

while($row = mssql_fetch_array($result))
{
If ($row[0] != "Cell 20")
{
echo "<tr><td><p class=other>" . $row[0] . "</td>";
echo "<td><p class=other><input type=text name=HEOne" . $i ." size=10 onchange=ChangeHandler(" . $i . ")></td>";
echo "<td><p class=other><input type=text name=HETwo" . $i ." size=10 value=" . $row2[$i + 3] . "></td>";
echo "<td><p class=other><input type=text name=HEThree" . $i ." size=10></td></tr>";
}
$i++;
}

New function:

<script language="JavaScript 1.2" type="text/javascript">
<!--
Function ChangeHandler(i)
{
document.dform.HEThree" + i + ".value=(parseFloat(HEOne" + i + ".value) - parseFloat(HETwo" + i + ".value));
}
//-->
</script>

There were some definite mistakes that I made on the first try. Does anyone know what I'm doing wrong? :(

DeadEyes
Dec 22nd, 2003, 07:42 AM
off the top of my head if you call the function like this

ChangeHandler(this);

then change the function to

//obj is the textbox
ChangeHandler(obj){
obj.value =(parseFloat(HEOne" + i + ".value) - parseFloat(HETwo" + i + ".value));
}

ober0330
Dec 22nd, 2003, 07:47 AM
But that won't work. That wouldn't get the correct value of "i" into the function. These textboxes are dynamic in name and that's what I need help in interfacing with.

ober0330
Dec 22nd, 2003, 09:53 AM
TA DA!!! Check this out!! :D

$i=0;
while($row = mssql_fetch_array($result))
{
If ($row[0] != "Cell 20")
{
echo "<tr><td><p class=other>" . $row[0] . "</td>";
echo "<td><p class=other><input type=text name=\"HEOne" . $i ."\" size=10 onchange=\"ChangeHandler($i, document.dform.HEOne$i, document.dform.HETwo$i, document.dform.HEThree$i)\"></td>";
echo "<td><p class=other><input type=text name=\"HETwo" . $i ."\" size=10 value=" . $row2[$i + 3] . "></td>";
echo "<td><p class=other><input type=text name=\"HEThree" . $i ."\" size=10></td></tr>";
}
$i++;


<script language="JavaScript1.2" type="text/javascript">
<!--
Function ChangeHandler(someval, somebox0, somebox1, somebox2)
{
somebox2.value=(parseFloat(somebox0.value) - parseFloat(somebox1.value))
}
-->
</script>

CornedBee
Dec 27th, 2003, 05:51 AM
The function works? JavaScript is case sensitive, it should reject the incorrectly written Function keyword.

But I guess the problem were the missing quotes on the HTML property value, right? The HTML rule is simple: you may only omit the quotes if the attribute value consists ONLY of numbers OR letters. The XHTML rule is even simpler: you may never omit the quotes ;)

I'm also surprised that PHP accepts the If.

Don't forget that the document.name schema simply invites cross-browser troubles.

ober0330
Jan 2nd, 2004, 07:00 AM
I will change my capitalization.

What is the correct way to reference a textbox in a form without using "document.documentname.item"?

CornedBee
Jan 2nd, 2004, 09:23 AM
Give the element an id attribute and use getElementById:
var thebox = document.getElementById("id_of_box");

Doesn't work in NS4, but neither does your current stuff.