473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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.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]
Jul 20 '05 #1
4 2200
Sandman <mr@sandman.net> wrote in
news:mr**********************@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
Jul 20 '05 #2
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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3
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**********************@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
Jul 20 '05 #4
"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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: J.R | last post by:
Greetings, I'm adding dynamically created input type='file' controls via JavaScript. However when I try to access they do not seem to be returned in the form collection. Any ideas? Thanks,...
8
by: Neil Zanella | last post by:
Hello, I would like to know why the W3C decided not to make the following valid XHTML: <input checked type="radio" name="foo" value="bar" />FooBar forcing people to write the following to...
10
by: crjunk | last post by:
I have a script that I want to run only when my input box IS NOT disabled. Can someone tell me if something is wrong with my script? Is "disabled" the correct property to use? function...
0
by: Veli-Pekka Tätilä | last post by:
Hi, My first post here. I've found some serious accessibility flaws in the Python 2.4 docs and wish they could be rectified over time. I'm very new to Python and initially contacted docs at python...
1
by: John | last post by:
Hi What would be the input mask for a name field where first letter is caps and the rest lower? Its ideal if the mask can handle multiple names separated by a space but if not a single word will...
38
by: John Salerno | last post by:
Here's my script: import sqlite3 con = sqlite3.connect('labdb') cur = con.cursor() cur.executescript(''' DROP TABLE IF EXISTS Researchers; CREATE TABLE Researchers ( researcherID...
6
by: effendi | last post by:
I have an array for of input names. Input1 , input 2 etc. Can I pass the input names in a loop like document.all.value="xxxx" The problem is document.all cannot be used in Safari/firefox, I...
13
by: Lee | last post by:
I have this function that doesn't work. I pass it the td element and an id, and it makes an input field inside the td. That part workds. What doesn't work is that I want to add an "onkeyup" on...
10
by: Jaye | last post by:
Hi. I am a relative newbie to ASP and I am working on an application that uses ASP and an Oracle 9i database. I have a form that allows the user to query the database by selecting a client name(s)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.