input names with '-' in their names.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sandman

    input names with '-' in their names.

    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.i nput.selectedIn dex = 0

    But this won't work:

    document.form.b orn-year.selectedIn dex = 0

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

    --
    Sandman[.net]
  • Sean Jorden

    #2
    Re: input names with '-' in their names.

    Sandman <mr@sandman.net > wrote in
    news:mr-0E2F56.10311103 092003@news.fu-berlin.de:
    [color=blue]
    > 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.i nput.selectedIn dex = 0
    >
    > But this won't work:
    >
    > document.form.b orn-year.selectedIn dex = 0
    >
    > Probably because it has a '-' in it's name, right? How do I do it?
    >[/color]

    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

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: input names with '-' in their names.

      Sandman <mr@sandman.net > writes:
      [color=blue]
      > But this won't work:
      >
      > document.form.b orn-year.selectedIn dex = 0
      >
      > Probably because it has a '-' in it's name, right? How do I do it?[/color]

      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 - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • The Plankmeister

        #4
        Re: input names with '-' in their names.

        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.getEle mentById("born-year").selected Index = 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" <mr@sandman.net > wrote in message
        news:mr-0E2F56.10311103 092003@news.fu-berlin.de...[color=blue]
        > 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[/color]
        is[color=blue]
        > done by
        >
        > document.form.i nput.selectedIn dex = 0
        >
        > But this won't work:
        >
        > document.form.b orn-year.selectedIn dex = 0
        >
        > Probably because it has a '-' in it's name, right? How do I do it?
        >
        > --
        > Sandman[.net][/color]


        ---
        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


        Comment

        • Lasse Reichstein Nielsen

          #5
          Re: input names with '-' in their names.

          "The Plankmeister" <plankmeister_N O_@_SPAM_hotmai l.com> writes:
          [color=blue]
          > 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">[/color]

          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.
          [color=blue]
          > 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.[/color]

          Another reason to keep name and id separate.

          /L
          --
          Lasse Reichstein Nielsen - lrn@hotpop.com
          Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
          'Faith without judgement merely degrades the spirit divine.'

          Comment

          Working...