473,396 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Custom function

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
Mar 25 '06 #1
5 1220
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
Mar 25 '06 #2
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

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 25 '06 #3
Bundy wrote:
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.form1[fieldname].style.visibility = 'visible';

--
Ian Collins.
Mar 25 '06 #4
RobG wrote:
[...]
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
Mar 25 '06 #5
It works!

Thank you for your help.

Bundy
Mar 25 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
5
by: Jason Butera | last post by:
I know that I can read/write custom properties of an object by using the following: Setting: document.all.customProp = "this"; Getting: document.all.customProp; Is there a way I can run...
22
by: TC | last post by:
I have an Access database application with a lot of custom row functions written in VBA. In other words, a lot of queries contain calculated fields which use functions defined in the modules. I...
8
by: Tinus | last post by:
Hello all, I've create a custom control (UserControl) and have a custom Item Collection. The control is a custom calendar which is draw using the Graphics Rectangle etc. functions. It is drawn...
15
by: Tinus | last post by:
Hello all, I've created a custom control that draws a monthly schedule (using the Draw function in C#). Basically it draws 31 boxes and writes the day number in every box. This works...
21
by: One Handed Man \( OHM - Terry Burns \) | last post by:
When using a custom control. In order to check and see if values have changed one has to implement the IPostBackDataCollection interface. The values returned for the control seem to be simply a...
1
by: nate axtell | last post by:
In VB .Net I made a custom CheckBox column style (for the Datagrid control) that maps to two DataTable columns , one it uses for the Checked status and the other it uses for the Enabled status. I am...
2
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
0
by: Pieter | last post by:
Hi, I'm using NHibernate 1.2 (CR1), and I'm using a custom list (inherited from BindingList(Of T) ) for all my lists. The NHibernate documentation told me that I had to implement...
3
by: skcsknathan001 | last post by:
I have 2 projects, and accessing a custom control , fron another project. Inside the custom control, it calls a function It compiles and when it runs I get a NullReference error message on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.