input names with '-' in their names.

Discussion in 'Javascript' started by Sandman, Sep 3, 2003.

  1. Sandman

    Sandman Guest

    I have a input like this:
    <select name="born-year">
    [list of years]
    </select>

    And I want to set this input to the first in selectedIndex, which usually is
    done by

    document.form.input.selectedIndex = 0

    But this won't work:

    document.form.born-year.selectedIndex = 0

    Probably because it has a '-' in it's name, right? How do I do it?

    --
    Sandman[.net]
    Sandman, Sep 3, 2003
    #1
    1. Advertising

  2. Sandman

    Sean Jorden Guest

    Sandman <> wrote in
    news:-berlin.de:

    > I have a input like this:
    > <select name="born-year">
    > [list of years]
    > </select>
    >
    > And I want to set this input to the first in selectedIndex, which
    > usually is done by
    >
    > document.form.input.selectedIndex = 0
    >
    > But this won't work:
    >
    > document.form.born-year.selectedIndex = 0
    >
    > Probably because it has a '-' in it's name, right? How do I do it?
    >


    although you could do it with document.forms[0].elements['born-users'] it
    is a far far better practice to stick naming your elements using
    alphanumerics and underscores only.

    --
    In theory there is no difference between theory and practice. In practice
    there is. - YB
    Sean Jorden, Sep 3, 2003
    #2
    1. Advertising

  3. Sandman <> writes:

    > But this won't work:
    >
    > document.form.born-year.selectedIndex = 0
    >
    > Probably because it has a '-' in it's name, right? How do I do it?


    I recommend always using the forms and elements collections, and use
    square-bracket-notation:
    document.forms['form'].elements['born-year'].selectedIndex = 0;
    I prefer this, since it keeps the namespaces (Javascript DOM names and
    document names) separate.

    How to access properties that are not legal Javascrip identifiers is
    in the FAQ: <URL:http://jibbering.com/faq/#FAQ4_25>

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 3, 2003
    #3
  4. I would suggest giving the tag an identical id attribute, then using the
    getElementById() function to get it, thusly:

    <select name="born-year" id="born-year">
    blah....
    </select>

    .....
    .....

    document.getElementById("born-year").selectedIndex = 0;

    Using this method, you can directly access any id'd object in your page,
    whether it's in a form or not. Obviously this means that if you have two
    similar forms, they can't share identically named tags.

    HTH,

    P.


    "Sandman" <> wrote in message
    news:-berlin.de...
    > I have a input like this:
    > <select name="born-year">
    > [list of years]
    > </select>
    >
    > And I want to set this input to the first in selectedIndex, which usually

    is
    > done by
    >
    > document.form.input.selectedIndex = 0
    >
    > But this won't work:
    >
    > document.form.born-year.selectedIndex = 0
    >
    > Probably because it has a '-' in it's name, right? How do I do it?
    >
    > --
    > Sandman[.net]



    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.514 / Virus Database: 312 - Release Date: 28/08/2003
    The Plankmeister, Sep 4, 2003
    #4
  5. "The Plankmeister" <plankmeister_NO_@_SPAM_hotmail.com> writes:

    > I would suggest giving the tag an identical id attribute, then using the
    > getElementById() function to get it, thusly:
    >
    > <select name="born-year" id="born-year">


    I recommend against making the name and id idential. It is safer to
    distinguish the two, e.g., by using id="born-yead-ID" or something
    similar.

    > Using this method, you can directly access any id'd object in your page,
    > whether it's in a form or not. Obviously this means that if you have two
    > similar forms, they can't share identically named tags.


    Another reason to keep name and id separate.

    /L
    --
    Lasse Reichstein Nielsen -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Sep 4, 2003
    #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. Ashraf Fouad
    Replies:
    2
    Views:
    535
    Ashraf Fouad
    Jan 8, 2004
  2. Michael Krasnyk

    Print variable values by their names

    Michael Krasnyk, Oct 18, 2004, in forum: Python
    Replies:
    1
    Views:
    263
    Peter Otten
    Oct 18, 2004
  3. Noah
    Replies:
    2
    Views:
    260
  4. jeremy
    Replies:
    1
    Views:
    433
    Owen Jacobson
    Jul 3, 2007
  5. Sfdesigner Sfdesigner
    Replies:
    5
    Views:
    151
    Chris Shea
    Aug 13, 2007
Loading...

Share This Page