Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Custom function

Reply
Thread Tools

Custom function

 
 
Bundy
Guest
Posts: n/a
 
      03-25-2006
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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      03-25-2006
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
 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      03-25-2006
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/
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      03-25-2006
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
 
Reply With Quote
 
Bundy
Guest
Posts: n/a
 
      03-25-2006
It works!

Thank you for your help.

Bundy
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
calling my custom function same as library function Deniz Bahar C Programming 43 03-02-2005 10:18 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments