Custom function

B

Bundy

Hello

I have created a function.

function unHide (fieldname)
{
.... not important...
document.form1.fieldname.style.visibility = 'visible';

}

I have a field name called country in a form.
When the onclick="unHide('country')" is used then the above function
does not work
but if i change the following line within the function
document.form1.country.style.visibility = 'visible'; then it works.

What am i doing wrong.

Newbie so please keep it simple.

Regards

Bundy
 
R

RobG

Bundy said:
Hello

I have created a function.

function unHide (fieldname)
{
... not important...
document.form1.fieldname.style.visibility = 'visible';

}

That will attempt to access the fieldname property of the form1 elements
collection, and is equivalent to:

document.forms['form1'].elements['fieldname']

I have a field name called country in a form.
When the onclick="unHide('country')" is used then the above function
does not work
but if i change the following line within the function
document.form1.country.style.visibility = 'visible'; then it works.

What am i doing wrong.

If you want the local variable 'fieldname' to be evaluated and its value
used for the element name, use square brackets *without* quotes:

document.forms['form1'].elements[fieldname]


If you want the string 'country' to be used literally for the property,
either use it with square brackets and quotes, or use dot notation:

document.forms['form1'].elements['country']

or

document.form1.country


You can mix them too:

document.form1.elements['country']


Using dot notation restricts the characters that can be used for the
element name to strings that are also valid property names (e.g. you can't
use dots or characters like []).

If you use square bracket notation, you can use any character that is valid
in an element name, i.e. CDATA (which is pretty much anything you like,
including dots, brackets, dashes, etc.)

<URL:http://www.w3.org/TR/html4/types.html#type-cdata>


So while dot notation is convenient, it is restrictive and hard-codes
property names in the script. Square brackets makes the code longer, but
allows the use of local variables and a much greater range of characters in
the element name.
 
R

Randy Webb

Bundy said the following on 3/24/2006 9:19 PM:
Hello

I have created a function.

function unHide (fieldname)
{
.... not important...
document.form1.fieldname.style.visibility = 'visible';

}

I have a field name called country in a form.
When the onclick="unHide('country')" is used then the above function
does not work
but if i change the following line within the function
document.form1.country.style.visibility = 'visible'; then it works.

What am i doing wrong.

Newbie so please keep it simple.

document.forms['form1'].elements[fieldname]

Note the lack of quotes around fieldname
 
T

Thomas 'PointedEars' Lahn

RobG said:
[...]
If you want the string 'country' to be used literally for the property,
either use it with square brackets and quotes, or use dot notation:

document.forms['form1'].elements['country']

or

document.form1.country


You can mix them too:

document.form1.elements['country']


Using dot notation restricts the characters that can be used for the
element name to strings that are also valid property names (e.g. you
can't use dots or characters like []).

If you use square bracket notation, you can use any character that is
valid in an element name, i.e. CDATA (which is pretty much anything you
like, including dots, brackets, dashes, etc.)

<URL:http://www.w3.org/TR/html4/types.html#type-cdata>


So while dot notation is convenient, it is restrictive and hard-codes
property names in the script. Square brackets makes the code longer, but
allows the use of local variables and a much greater range of characters
in the element name.

And square bracket notation is standards compliant regarding the
DOM, while dot notation, at least the short form above, is not.

<URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top