Custom function

Discussion in 'Javascript' started by Bundy, Mar 25, 2006.

  1. Bundy

    Bundy Guest

    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
     
    Bundy, Mar 25, 2006
    #1
    1. Advertising

  2. Bundy

    RobG Guest

    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
     
    RobG, Mar 25, 2006
    #2
    1. Advertising

  3. Bundy

    Randy Webb Guest

    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/
     
    Randy Webb, Mar 25, 2006
    #3
  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
     
    Thomas 'PointedEars' Lahn, Mar 25, 2006
    #4
  5. Bundy

    Bundy Guest

    Re: [solved] Custom function

    It works!

    Thank you for your help.

    Bundy
     
    Bundy, Mar 25, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. James Vanns
    Replies:
    7
    Views:
    7,174
    Evan Carew
    Jan 21, 2004
  2. komal
    Replies:
    6
    Views:
    1,469
    msalters
    Jan 25, 2005
  3. Replies:
    2
    Views:
    972
    Bengt Richter
    Aug 1, 2005
  4. Deniz Bahar
    Replies:
    43
    Views:
    994
    Gordon Burditt
    Mar 2, 2005
  5. Giannis Papadopoulos

    Function pointer to void function and int function

    Giannis Papadopoulos, Sep 5, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,288
    Barry Schwarz
    Sep 5, 2005
Loading...

Share This Page