Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Javascript document.all for input tags that have array IDs

Status
Not open for further replies.

jgannaway

Programmer
Jan 28, 2005
4
US
Easier to show than explain.

I've got an HTML page with a bunch of drop-down lists (<SELECT> tags). There's a dropdown for each state. Each drop down contains <OPTION> for each county within that sate.
Code:
<SELECT NAME="county[AK]" ID="county[AK]">
 <OPTION>Alaska county 1</OPTION>
 <OPTION>Alaska county 2</OPTION>
 <OPTION>Alaska county 3</OPTION>
 ...
</SELECT>

<SELECT NAME="county[AL]" ID="county[AL]">
 <OPTION>Alabama county 1</OPTION>
 <OPTION>Alabama county 2</OPTION>
 <OPTION>Alabama county 3</OPTION>
 ...
</SELECT>

I have another drop down list of each state:
Code:
<SELECT NAME="states" ID="states" onChange="ShowCounty()">
 <OPTION VALUE="AK">AK - Alaska</OPTION>
 <OPTION VALUE="AL">AL - Alabama</OPTION>
 ...
</SELECT>

Basically, when the user selects a state, the ShowCounty() javascript function will:
1. hide all <SELECT> inputs with a "county[]" ID
2, show the <SELECT> input with the ID "county[document.all.states.value]"

The problem I'm running into is that Javascript doesn't seem to like using my SELECT tags that have array IDs. I can't even get the value of the drop-downs:

command: alert(document.all.county[AL].value);
error: 'AL' is undefined.

command: alert(document.all.county['AL'].value);
error: 'document.all.county.AL' is null or not an object.

I'd also like to be able to for-each through the array, but can't figure out how to do it Java Script.

For example in PHP I could:
Code:
foreach ($county as $key => $value)
{
  // $key would return the array key
  // $value would return the value
}


Is JavaScript incapable of dealing with objects with array names? If so, tell me what I need to do, please.

Thanks in advance!
-Jeff

"Grace... She travels outside of Karma"
-U2
 
2 things:

1. '[' and ']' are illegal characters for names and IDs in HTML. You should NOT use them. As you can see - they do not work.

2. You should avoid using "document.all" as it is IE-only. Unless you are writing this for in intranet where you know your users are only ever going to be using IE, you're really going to annoy users of Firefox, Safari, Netscape, Opera, etc.

Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Jeff,
I feel your pain. I had to work with PHP once. I can't believe they require you to use that syntax. Either use document.getElementById(), or use the method found on faq216-3858.

Adam
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top