/    Sign up×
Community /Pin to ProfileBookmark

Function arguments madness.

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().

[CODE]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;
}
}
}[/CODE]

Can what I’m asking be done? Inserting a variable inside a DOM pointer? And if so, how?

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@khalidali63Feb 20.2003 — 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
Copy linkTweet thisAlerts:
@Dan_DrillichFeb 21.2003 — The following seems to work as well -

<i>
</i>document.interface[arguments[i]].disabled = true;
Copy linkTweet thisAlerts:
@AttilaauthorFeb 21.2003 — Thx for your suggestions everyone, but I still get an error:

[CODE]Statement on line 13: Expression evaluated to null or undefined and is not convertible to Object: document.interface.elements[arguments[i]][/CODE]

This is the new code I'm using:

[CODE]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.
}
}
}[/CODE]


The same error even when it's dumbed down to something like this:

[CODE]<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>[/CODE]




Khalid(khalidali63), I can't reach that website, seems to be down, could you post the script here?
Copy linkTweet thisAlerts:
@AttilaauthorFeb 21.2003 — 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()):

[CODE]<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>[/CODE]


Thx again everyone!
Copy linkTweet thisAlerts:
@Dan_DrillichFeb 22.2003 — 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;
×

Success!

Help @Attila spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...