Click to See Complete Forum and Search --> : Function arguments madness.
Attila
02-20-2003, 12:40 PM
I want to create a javascript function that takes names of fields (in this case three, but any number should be possible), and disables those fields inside the interface form.
This function doesn't work, because Javascript probably starts looking for the arguments array property of interface, instead of using the arguments array used by disable_field().
function disable_field(field, field2, field3)
{
if(document.all)
{
for(var i=0; i<arguments.length; i++)
{
/* Creates error because js looks for interface.arguments[] */
document.interface.arguments[i].disabled = true;
}
}
}
Can what I'm asking be done? Inserting a variable inside a DOM pointer? And if so, how?
khalidali63
02-20-2003, 01:24 PM
Did you mean somethig along the lines as in this script.
It should not be hard at all to customise it for your needs.
http://68.145.35.86/skills/javascripts/DisableAllFormFields.html
cheers
Khalid
Dan Drillich
02-20-2003, 10:10 PM
The following seems to work as well -
document.interface[arguments[i]].disabled = true;
Attila
02-21-2003, 02:36 AM
Thx for your suggestions everyone, but I still get an error:
Statement on line 13: Expression evaluated to null or undefined and is not convertible to Object: document.interface.elements[arguments[i]]
This is the new code I'm using:
function disable_field(field, field2, field3)
{
if(document.all)
{
for(var i=0; i<arguments.length; i++)
{
document.interface.elements[arguments[i]].disabled = true; // Also tried interface[arguments[i]], same result.
}
}
}
The same error even when it's dumbed down to something like this:
<HTML>
<HEAD>
<SCRIPT>
<!--
function disable_field(field)
{
document.interface.elements[arguments[0]].disabled = true; // Also tried interface[arguments[i]], same result.
}
-->
</SCRIPT>
</HEAD>
<BODY>
<A href="#" onclick="disable_field(uno);"><IMG alt="" src="img/x.gif"/></A>
<FORM action="test.php" method="POST" name="interface">
<INPUT name="uno" type="text" value="number one"/>
<INPUT name="due" type="text" value="number two"/>
<INPUT name="" type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
Khalid(khalidali63), I can't reach that website, seems to be down, could you post the script here?
Attila
02-21-2003, 07:18 AM
That's it!
I simply forgot the singlequotes when calling the function!!
So the correct code is (btw, thx for that tip Dave, PHP has the same kind of thing with func_num_args() and func_get_args()):
<HTML>
<HEAD>
<SCRIPT>
<!--
function disable_field()
{
document.interface[arguments[0]].disabled = true;
}
-->
</SCRIPT>
</HEAD>
<BODY>
<?php
if($_POST)
{
print_r($_POST); // Just to print the array that was submitted, serverside.
echo(hello); // As you can see, PHP is much more forgiving in this area.
/* One would expect that the above line should read: echo('hello'); but without
* the quotes, it works just as well.
*/
}
?>
<A href="#" onclick="disable_field('uno');"><IMG alt="" src="img/x.gif"/></A>
<FORM action="test.php" method="POST" name="interface">
<INPUT name="uno" type="text" value="nummer een"/>
<INPUT name="due" type="text" value="nummer twee"/>
<INPUT name="" type="submit" value="submit"/>
</FORM>
</BODY>
</HTML>
Thx again everyone!
Dan Drillich
02-21-2003, 08:16 PM
Just for the record, the following also works –
eval('document.interface.' + arguments[i]).disabled = true;
So, in this thread we saw the following three ways to access the elements. In addition, these three ways are level 0 DOM (as far as I can tell) –
1) document.interface.elements[arguments[i]].disabled = true;
2) document.interface[arguments[i]].disabled = true;
3) eval('document.interface.' + arguments[i]).disabled = true;