can element id be an array?

C

Chamomile

For simplicity in referencing elements ( in this case div's) for
manipulating with javascript I have been using an array as the value for
id's.
eg:
<div id='mydiv[0]' >my div 0</div>
<div id='mydiv[1]'>my div 1</div>

this works fine even using 2 dimensional arrays for some fairly complicated
stuff..

However, one 'section' of a page started giving javascript errors (IE6)
although it used an identical code style.
After exhaustive cheking for the usual errors with no obvious culprits I ran
the page through a validator with the result that I had line after line of
complaints from the validator that the '[' character could not be used in an
id value.

If that's the case then why do some work? Can one really not use an array
member as an id?
 
J

Jukka K. Korpela

Chamomile said:
For simplicity in referencing elements ( in this case div's) for
manipulating with javascript I have been using an array as the
value for id's.
eg:
<div id='mydiv[0]' >my div 0</div>
<div id='mydiv[1]'>my div 1</div>

There are no arrays in HTML. The content of an id attribute is just an
identifier, with no internal structure from HTML viewpoint. Those
values are malformed, since the brackets are not permitted in
identifiers (attributes with ID value).
this works fine

Maybe on sunny days if the browser likes you. It's still incorrect
markup.

I suggest that you find some other method to iterate over a set of
elements in JavaScript. Try comp.lang.javascript after checkings its
FAQs.
 
J

Jeff Thies

For simplicity in referencing elements ( in this case div's) for
manipulating with javascript I have been using an array as the
value for id's.
eg:
<div id='mydiv[0]' >my div 0</div>
<div id='mydiv[1]'>my div 1</div>

Just do this:

id="id1"

The javascript would be : var i=1;document.getElementById('id'+i);

Jeff
 
R

Richard

Jeff said:
For simplicity in referencing elements ( in this case div's) for
manipulating with javascript I have been using an array as the
value for id's.
eg:
<div id='mydiv[0]' >my div 0</div>
<div id='mydiv[1]'>my div 1</div>
Just do this:

The javascript would be : var i=1;document.getElementById('id'+i);

And if the visitor has a non javascript browser, you're screwed.
 
C

Chamomile

I take the point about using an 'addon' digit rather than an array - and it
works ok,
though there are some things that I obviously won't then be able to do.

The point about non-javascript browsers is true, but there will be an
alternative.
(its a kind of dynamic text -tree thing which assumes a javascript enabled
browser.)

What puzzles me is why it works using the[n] array format at all!
It's only the validator that alerted me to it.
Are the browsers just treating it as a text string anyway? (after all, they
are different as strings)
and being kind about the illegal '[' character?
It works fine in IE, Netscape and Opera.
I have also used similar arrays before in html id's in long lists generated
by php
for web editing databases and that works fine too.
If it is the case that [] is illegal in an id then I have been innocently
using it with no problem for some time
I suppose the first time I tried it, it worked so I assumed it was ok.

footnote:
(by the way, the original script was working ok, I had one of those
cant-see-for-looking typos
that was causing the error that sent me to the validator in the first
place)

Richard said:
Jeff said:
For simplicity in referencing elements ( in this case div's) for
manipulating with javascript I have been using an array as the
value for id's.
eg:
<div id='mydiv[0]' >my div 0</div>
<div id='mydiv[1]'>my div 1</div>
Just do this:

The javascript would be : var i=1;document.getElementById('id'+i);

And if the visitor has a non javascript browser, you're screwed.
 
Joined
Oct 28, 2013
Messages
1
Reaction score
0
I am not challenging the fact that brackets are illicit in html. However, I've been using them for over 5 years, and it works fine for all my purposes.

Here is an example that works in all the usual browsers:

<html>...
<form...>
<input id="myTestId[illicitId]" name="myTestId[illicitId]" type="text" value="nothing" onclick="javascript:TestAssignValue('illicitId')">
</form>

<script ...>
function TestAssignValue(illicitId)
{
var tmpvar = 'myTestId['+illicitId+']';
document.getElementById(tmpvar).value='test';
}
</script>

is equivalent to

<script ...>
document.getElementById('myTestId['+illicitId+']').value='test';
</script>

and assigns the value: "test" to myTestId[illicitId] (id or/and name)

You may loop it as much as you want. :)
 
Last edited:

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top