Bundy wrote:
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.
--
Rob