Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #16264

Re: document.elementname vs document.getElementById("id")

From "Jukka K. Korpela" <jkorpela@cs.tut.fi>
Newsgroups comp.lang.javascript
Subject Re: document.elementname vs document.getElementById("id")
Date 2012-09-30 11:02 +0300
Organization A noiseless patient Spider
Message-ID <k48ubc$ab6$1@dont-email.me> (permalink)
References <k47drn$q7j$1@dont-email.me> <k48a8p$he5$1@speranza.aioe.org>

Show all headers | View raw


2012-09-30 5:20, J.R. wrote:

> On 29/09/2012 15:15, Denis McMahon wrote:
>> Gaaah
>>
>> I'm fairly certain there's a reason that I think this is a "bad" idea ...
>> apart from anything else an element name parameter is not guaranteed to
>> be unique within a document - but I haven't easily been able to find a
>> suitable reference for why not to do this, anyone got one?
>
> Perhaps you are thinking of an ID instead of a name attribute.

The question, including the Subject line, is somewhat confused, but the 
question is sufficiently clearly formulated in the text you quoted next:

>> I'm arguing to replace using document.formname to identify forms with
>> assigning an id to the forms involved and using document.getElementById
>> ("formid").*

So there's no "perhaps" about it: pages are now using <form ... 
name="foobar"> in HTML and document.foobar in JavaScript and would be 
using <form ... id="zipzap"> in HTML and 
document.getElementById('zipzap') in JavaScript.

The short answer is: change your habits but not existing code. For new 
pages, use the modern, logical way. For old pages, remember that any fix 
will introduce errors with a nonignorable probability, so don't do fixes 
just to "clean things up"

> document.formname is okay with HTML 4.

Actually, no. Check it out, from the specs or using a validator. HTML 4, 
i.e. 4.0, does not allow the name attribute for <form>, and the spec 
says nothing about allowed constructs in JavaScript. HTML 4 is 
effectively forgotten, and better kept that way. And HTML 4.01 has just 
a few changes from it, but they include this part: <form ... name="..."> 
is valid in HTML 4.01.

> You may use the W3C Markup
> Validation Service to check if an element should have an ID.

How would you do that? The id attribute is not *required* in any version 
of HTML.

>> I'm pretty certain that using document.getElementById("id") is better
>> than using document.elementname, but at the moment I'm having trouble
>> pointing to any sort of authoritative reference for this.

It should not be hard to find an authoritative reference: HTML 4.01 
spec, describing the <form> element, says the following about the name 
attribute:

"This attribute names the element so that it may be referred to from 
style sheets or scripts. Note. This attribute has been included for 
backwards compatibility. Applications should use the id attribute to 
identify elements."

http://www.w3.org/TR/html401/interact/forms.html#adef-name-FORM

> Interestingly, Firefox still allows
> references in the old way (document.elementID or formName.elementName)
> when a page is rendered in "quirks mode".

You are confusing support to document.foobar (when accessing a <form> 
element that has name="foobar") with issues like making id attribute 
values properties of the document object. The topic is confusing enough 
without such distractions.

> Yes, this sort of "fix" is error prone. But a form's ID is not required
> in HTML 4 - only XHTML pages should require IDs:
> <http://www.w3.org/TR/xhtml1/diffs.html>

The id attribute is not required in any version of HTML, including any 
version of XHTML.

The language of W3C documents is sometimes sloppy and may mislead you if 
you read it carelessly and without reading the context. What
http://www.w3.org/TR/xhtml1/diffs.html#h-4.10
(as well as the HTML 4.01 spec clause I mentioned earlier) is trying to 
say that you should not use the name attribute of a <form> element to 
refer to the element (which is the only reason for having that attribute 
in the first place) but some other, more logically designed way of 
identifying an element. The id attribute just happens to be the most 
handy tool for the purpose in simple cases.

There are many other ways to identify an element, and you may need to 
use one e.g. when you just cannot modify the HTML source of a document 
for some reason. For example, most pages have just a single form 
element, and then you can simply use

document.getElementsByTagName('form')[0]

This is however not quite as robust as using an id attribute, since some 
day someone might add another <form> element and place it before yours. 
(In balance, someone might also add another element with the same id 
attribute. That would be a reportable markup error, e.g. would be 
flagged by a validator, but how many people actually validate their 
markup after each and every change?)

-- 
Yucca, http://www.cs.tut.fi/~jkorpela/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-29 18:15 +0000
  Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-29 22:17 +0200
  Re: document.elementname vs document.getElementById("id") dann90038@gmail.com - 2012-09-29 16:21 -0700
  Re: document.elementname vs document.getElementById("id") "J.R." <groups_jr-1@yahoo.com.br> - 2012-09-29 23:20 -0300
    Re: document.elementname vs document.getElementById("id") "J.R." <groups_jr-1@yahoo.com.br> - 2012-09-29 19:53 -0700
    Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 11:02 +0300
      Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-30 13:10 +0000
      Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-30 13:14 +0000
        Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 17:20 +0300
          Re: document.elementname vs document.getElementById("id") Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-09-30 21:09 +0200
            Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 22:28 +0300
              Re: document.elementname vs document.getElementById("id") Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2012-09-30 21:53 +0200
            Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 21:53 +0200
          Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-09-30 22:07 +0000
            Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-10-01 06:23 +0300
              Re: document.elementname vs document.getElementById("id") Denis McMahon <denismfmcmahon@gmail.com> - 2012-10-01 08:11 +0000
                Re: document.elementname vs document.getElementById("id") Stefan Weiss <krewecherl@gmail.com> - 2012-10-01 11:25 +0200
      Re: document.elementname vs document.getElementById("id") "J.R." <groups_jr-1@yahoo.com.br> - 2012-09-30 15:10 -0300
        Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 20:43 +0200
    Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 12:22 +0200
      Re: document.elementname vs document.getElementById("id") "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2012-09-30 13:44 +0300
        Re: document.elementname vs document.getElementById("id") Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-30 13:46 +0200
  Re: document.elementname vs document.getElementById("id") Dr J R Stockton <reply1239@merlyn.demon.co.uk.invalid> - 2012-09-30 20:10 +0100

csiph-web