input names with '-' in their names.

S

Sandman

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

Sean Jorden

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

Lasse Reichstein Nielsen

Sandman said:
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
 
T

The Plankmeister

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 said:
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?
 
L

Lasse Reichstein Nielsen

The Plankmeister said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top