handling input array names

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

handling input array names

Post by kendall »

hello,

i didnt really know how to term this issue so please excuse me...

i have several checkboxes all id as

Code: Select all

<input type="checkbox" id="region[]" name="region[]" />
How does javascript access an element with id like that?
Kendall
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It doesn't very easily, sorta. getElementsByTagName can pull up an arrary of the checkboxes. the id attribute is supposed to be unique. So if you created them in a linear way (say region1, region2, region3) you could iterate over them quite easilying using getElementById then:

Code: Select all

var foo = 1;
while(bar = document.getElementById('region'+(foo++)))
{
  // do something
}
Post Reply