Connecting Tech Pros Worldwide Forums | Help | Site Map

input names with '-' in their names.

Sandman
Guest
 
Posts: n/a
#1: Jul 20 '05
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]

Sean Jorden
Guest
 
Posts: n/a
#2: Jul 20 '05

re: input names with '-' in their names.


Sandman <mr@sandman.net> wrote in
news:mr-0E2F56.10311103092003@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.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?
>[/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
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#3: Jul 20 '05

re: input names with '-' in their names.


Sandman <mr@sandman.net> writes:
[color=blue]
> 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?[/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.'
The Plankmeister
Guest
 
Posts: n/a
#4: Jul 20 '05

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


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#5: Jul 20 '05

re: input names with '-' in their names.


"The Plankmeister" <plankmeister_NO_@_SPAM_hotmail.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.'
Closed Thread


Similar JavaScript / Ajax / DHTML bytes