input names with '-' in their names.

  • Follow


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]
0
Reply Sandman 9/3/2003 8:31:11 AM

Sandman <mr@sandman.net> wrote in
news:mr-0E2F56.10311103092003@news.fu-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
0
Reply Sean 9/3/2003 8:38:48 AM


Sandman <mr@sandman.net> 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  -  lrn@hotpop.com
 Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Reply Lasse 9/3/2003 4:08:58 PM

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" <mr@sandman.net> wrote in message
news:mr-0E2F56.10311103092003@news.fu-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


0
Reply The 9/4/2003 6:55:01 AM

"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  -  lrn@hotpop.com
 Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
  'Faith without judgement merely degrades the spirit divine.'
0
Reply Lasse 9/4/2003 8:11:31 AM

4 Replies
58 Views

(page loaded in 0.901 seconds)


Reply: