473,396 Members | 1,997 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,396 software developers and data experts.

using arrays in html id's

I have been happily using array members as id's in my html code
(either hand coded or generated by server-side script-php ) for some time.

eg < input type='text' id='arrayItem[0]' >< input type='text'
id='arrayItem[1]' >etc.

particlarly in forms for sending input data to be server processed.
and also using javascript client-side for dynamic text menu-trees etc.
This has worked fine using IE Netscape and Opera as test browsers.

However, I recently had cause to run such a page through a validator for
some unconnected reason and it complained that the '[' symbol was illegal!

That is, using a 'raw' array element was not right in html.
So, why does it work? Should I stop doing it?
Is the id='arrayItem[n]' being treated as a text string rather than as an
array?
In which case should I dispense with the [] and just use an added (text)
number identifier and then parse that
using javascript or my server side language to work with it?
Working on the principle that 'if it ain't broke dont fix it'
(and I've got code out there happily working using [] in element id's)
should I change my ways and re-think how I've been programming this type of
application
all for the sake of not upsetting a validator?
any thoughts?
Jul 20 '05 #1
10 13383
"Chamomile" <ne********@chamomile.co.uk> writes:
eg < input type='text' id='arrayItem[0]' >< input type='text'
id='arrayItem[1]' >etc. .... However, I recently had cause to run such a page through a validator for
some unconnected reason and it complained that the '[' symbol was illegal!
It is.
<URL:http://www.w3.org/TR/html4/types.html#type-name>

However, the "id" attribute is only meant for the client. Your example
uses input elements, and when used as part of a form, you would want
to use the "name" attribute, which doesn't have that restriction (its
content type is CDATA, not NAME as one might expect).

On input elements, and other form controls, the control name ("name"
attribute") and id ("id" attribute) doesn't have to be the same.
PHP is happy to convert names that look like arrays (name="arrayName[]")
to arrays on the server.
That is, using a 'raw' array element was not right in html.
So, why does it work?
Because browsers are made to compensate for authors who can't, or
won't, write correct HTML.
Should I stop doing it?
Absolutely.
Is the id='arrayItem[n]' being treated as a text string rather than as an
array?
Ofcourse. The content type if the id attribute is ID. It is not just a
string, it is a very restricted string (see URL above).

What did you expect it to do?
In which case should I dispense with the [] and just use an added
(text) number identifier and then parse that using javascript or my
server side language to work with it?
That is a solution.
Working on the principle that 'if it ain't broke dont fix it' (and
I've got code out there happily working using [] in element id's)
should I change my ways and re-think how I've been programming this
type of application all for the sake of not upsetting a validator?
Don't think of it as "upsetting the validator" as much as "upsetting
potential future standards-compliant browsers". Or just "not writing
(correct) HTML 4".
any thoughts?


Write correct HTML. If a browser breaks on that, you can rightfully
blame the browser. If a new browser decided to break on your illegal
id tokens, you would have only yourself to blame. I'd use defensive
programming, especially since it is so easy to do.

So, make your id's correct. Use array notation for form control's
control names.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Thanks for the lesson.
To clarify if I've got this right:.

If I'm naming, say <div>'s to be manipulated by javascript on the client
I dont use array elements as id's but I can manipulate the element using
parameters sent from function calls
by using concatenated stuff like id= 'name'+var to give an id name like
id='myId0' or 'myId1' etc.

If I'm doing form elements then its ok to use arrays but with a
name='array[n]' not id='array[n]'

is that it?

thanks
mjg

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:is**********@hotpop.com...
"Chamomile" <ne********@chamomile.co.uk> writes:
eg < input type='text' id='arrayItem[0]' >< input type='text'
id='arrayItem[1]' >etc. ...
However, I recently had cause to run such a page through a validator for
some unconnected reason and it complained that the '[' symbol was illegal!
It is.
<URL:http://www.w3.org/TR/html4/types.html#type-name>

However, the "id" attribute is only meant for the client. Your example
uses input elements, and when used as part of a form, you would want
to use the "name" attribute, which doesn't have that restriction (its
content type is CDATA, not NAME as one might expect).

On input elements, and other form controls, the control name ("name"
attribute") and id ("id" attribute) doesn't have to be the same.
PHP is happy to convert names that look like arrays (name="arrayName[]")
to arrays on the server.
That is, using a 'raw' array element was not right in html.
So, why does it work?
Because browsers are made to compensate for authors who can't, or
won't, write correct HTML.
Should I stop doing it?


Absolutely.
Is the id='arrayItem[n]' being treated as a text string rather than as

an array?


Ofcourse. The content type if the id attribute is ID. It is not just a
string, it is a very restricted string (see URL above).

What did you expect it to do?
In which case should I dispense with the [] and just use an added
(text) number identifier and then parse that using javascript or my
server side language to work with it?


That is a solution.
Working on the principle that 'if it ain't broke dont fix it' (and
I've got code out there happily working using [] in element id's)
should I change my ways and re-think how I've been programming this
type of application all for the sake of not upsetting a validator?


Don't think of it as "upsetting the validator" as much as "upsetting
potential future standards-compliant browsers". Or just "not writing
(correct) HTML 4".
any thoughts?


Write correct HTML. If a browser breaks on that, you can rightfully
blame the browser. If a new browser decided to break on your illegal
id tokens, you would have only yourself to blame. I'd use defensive
programming, especially since it is so easy to do.

So, make your id's correct. Use array notation for form control's
control names.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #3
"Chamomile" <ne********@chamomile.co.uk> writes:

Please don't top post.
To clarify if I've got this right:.

If I'm naming, say <div>'s to be manipulated by javascript on the client
I dont use array elements as id's
You never use "[" as part of an id, so yes.
but I can manipulate the element using parameters sent from function
calls by using concatenated stuff like id= 'name'+var to give an id
name like id='myId0' or 'myId1' etc.
Yes. You can access them as, e.g.,
document.getElementById("myId"+n)
If I'm doing form elements then its ok to use arrays but with a
name='array[n]' not id='array[n]'


Yes. There is no restriction on the value of the "name" attribute. It
is the "name" attribute that defines the /control name/ of the
control, which is what is sent to the server when the form is submitted.

The "id" attribute only defines the internal name in the page, and
can't contain a "[".

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
what's 'top post'?

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:ad**********@hotpop.com...
"Chamomile" <ne********@chamomile.co.uk> writes:

Please don't top post.
To clarify if I've got this right:.

If I'm naming, say <div>'s to be manipulated by javascript on the client I dont use array elements as id's
You never use "[" as part of an id, so yes.
but I can manipulate the element using parameters sent from function
calls by using concatenated stuff like id= 'name'+var to give an id
name like id='myId0' or 'myId1' etc.


Yes. You can access them as, e.g.,
document.getElementById("myId"+n)
If I'm doing form elements then its ok to use arrays but with a
name='array[n]' not id='array[n]'


Yes. There is no restriction on the value of the "name" attribute. It
is the "name" attribute that defines the /control name/ of the
control, which is what is sent to the server when the form is submitted.

The "id" attribute only defines the internal name in the page, and
can't contain a "[".

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors:

<URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html> 'Faith without judgement merely degrades the spirit divine.'

Jul 20 '05 #5
On Sun, 25 Jan 2004 16:56:20 -0000, Chamomile <ne********@chamomile.co.uk>
wrote:
what's 'top post'?


By placing your replies above quoted material, you are "top-posting". If
you place them under, or amongst (when responding to a specific point),
you make your posts easier to read. Top-posting is like giving an answer
before even hearing the question. Also, you should trim what you quote to
only the text that is relevant to your reply. It also improves readability
and reduces the amount of data to download (which can be a factor for
dial-up users like me).

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #6
is that better?
(i'm afraid I let outlook express 'do it for me'[shame])
thanks for the pointer
mjg
Jul 20 '05 #7
On Sun, 25 Jan 2004 18:10:08 -0000, Chamomile <ne********@chamomile.co.uk>
wrote:
is that better?
You should try to keep some of the original content, including the
attribution line (On ..., ... wrote:) This allows people who might not
have received the post you are responding to, to understand the context of
your reply.

It's something you'll get used to.
(i'm afraid I let outlook express 'do it for me'[shame])
Outlook is one of several newsreaders that encourage top-posting, or at
least make it seem natural, by the initial layout of follow-on posts.
thanks for the pointer


You're welcome.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #8
JRS: In article <10*************@corp.supernews.com>, seen in
news:comp.lang.javascript, Chamomile <ne********@chamomile.co.uk> posted
at Sun, 25 Jan 2004 16:56:20 :-
what's 'top post'?


for such questions, and others, read the FAQ. That one is short for FAQ
Sec 2.3 para 5 sentence 6.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 20 '05 #9
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message news:op**************@news-text.blueyonder.co.uk...
On Sun, 25 Jan 2004 16:56:20 -0000, Chamomile <ne********@chamomile.co.uk> what's 'top post'?
By placing your replies above quoted material, you are "top-posting". If
you place them under, or amongst (when responding to a specific point),
you make your posts easier to read.
Lasse Reichstein Nielsen - lr*@hotpop.com


hmmm....I look at it this way. Of course this is not a criticsm just an explanation. I read a post. Then I move down to read the reply. Why then do I have to scour through looking for the reply? I read the original why am I presented with it again when I read the reply? Please I'd like at least one valid reason for this top-posting because easier is in the eyes of the beholder. It is much more confusing and difficult for me. Hell I don't want to read the message again after I just read it. So top posting sucks.

--
George Hester
__________________________________
Jul 20 '05 #10
On Wed, 28 Jan 2004 20:53:08 GMT, George Hester <he********@hotmail.com>
wrote:
"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
>On Sun, 25 Jan 2004 16:56:20 -0000, Chamomile <ne********@chamomile.co.uk>

> what's 'top post'?

By placing your replies above quoted material, you are "top-posting". If
you place them under, or amongst (when responding to a specific point),
you make your posts easier to read.
Lasse Reichstein Nielsen - lr*@hotpop.com


Where does Mr Nielsen fit in with my post? Please quote correctly in the
future.
hmmm....I look at it this way. Of course this is not a criticsm just an
explanation. I read a post. Then I move down to read the reply. Why
then do I have to scour through looking for the reply? I read the
original why am I presented with it again when I read the reply? Please
I'd like at least one valid reason for this top-posting because easier
is in the eyes of the beholder. It is much more confusing and difficult
for me. Hell I don't want to read the message again after I just read
it. So top posting sucks.


You can respond properly to individual points in someone's post. If I
wrote the line above ("Where does Mr Nielsen...") at the top of the post,
you would wonder what on Earth I was talking about. By placing it directly
after the text it comments upon, you can see immediately what I'm
referring to.

If a reference was more obscure, what would be more annoying: having to
hunt through a (potentially long) post, or reading a bottom-posted
message? I would think the former.

Another reason is that top-posting encourages reckless quoting. You (not
you, Mr Hester, specifically) write your answer, and that's that. It
doesn't occur that you left 80 lines of quoted material below, most of
which has nothing to do with your reply. Middle-posting (for want of a
better term), where you reply to each point in turn with a relevant quote
above, encourages the trimming of that surplus.

For the third reason, we have signatures. According to the message format
for Internet messages, the string "-- " denotes the beginning of a
signature. All that should exist after that point is the signature and
nothing more. Top-posting breaks this; all of the quoted message, in
addition to the signature, will appear as signature text.

Finally, you mentioned that you see no point in reading something in a
reply that you've already read. What if your news server hasn't yet
received a message in a thread, but you have received later messages[1]?
In order to understand the reply, you have to move to the bottom of the
post, read the earlier reply, then return to continue reading in the newly
understood context. When the whole text flows, there is no need to do this.

With a decent newsreader, you can't argue readability. XNews and Opera's
M2 client both use a highlighting system that gives replies at different
levels, different colours. If you don't need to read the quoted text, it's
easy to skip past the section in that colour.

Mike

[1] I've received posts around 8 hours after the initial posting time, and
no, I'm not confusing time-zones.

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #11

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

Similar topics

5
by: Golf Nut | last post by:
I am finding that altering and affecting values in elements in multidimensional arrays is a huge pain in the ass. I cannot seem to find a consistent way to assign values to arrays. Foreach would...
3
by: M.D. | last post by:
Hi, I am pretty new on php, and have been trying to solve a problem I am having for the past 2 days. I have a HTML page which is going to have a form and some of the fields in the form will...
7
by: Gary | last post by:
I haver a table of students - Say 100 students that I need to be able to update/delete and amend. I know I can do this one student at a time which is simple but lets say I want to see all the...
6
by: Xiaozhu | last post by:
say, if you had parallel arrays containing the sales person id, the month, and the sales figures (for that person in that month), sorting on sales figure and preserve the order of figures within...
8
by: dbaplusplus | last post by:
I worked on web development using java script many many years, so I am now a newbie to javascript. I have a single html page, which is generated dynamically using some programming language. Web...
16
by: Ian Davies | last post by:
Hello Needing help with a suitable solution. I have extracted records into a table under three columns 'category', 'comment' and share (the category column also holds the index no of the record...
3
by: SM | last post by:
Hello, Im trying to access elements in my XML file using the JavaScript DOM but i'm not sure how. I use AJAX to access the XML and then use the responseXML property to access the XML file data. I...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.