Best Practice: Access form elements by HTML id or name attribute?
As any seasoned JavaScript developer knows, there are many (too many) ways to do the same thing. For example, say you have a text field as follows:
<form name="myForm">
<input type="text" name="foo" id="foo" />
There are many way to access this in JavaScript:
[1] document.forms[0].elements[0];
[2] document.myForm.foo;
[3] document.getElementById('foo');
[4] document.getElementById('myForm').foo;
... and so on ...
Methods [1] and [3] are well documented in the Mozilla Gecko documentation, but neither are ideal. [1] is just too general to be useful and [3] requires both an id and a name (assuming you will be posting the data to a server side language). Ideally, it would be best to have only an id attribute or a name attribute (having both is somewhat redundant, especially if the id isn't necessary for any css, and increases the likelihood of typos, etc).
[2] seems to be the most intuitive and it seems to be widely used, but I haven't seen it referenced in the Gecko documentation and I'm worried about both forwards compatibility and cross browser compatiblity (and of course I want to be as standards compliant as possible).
So what's best practice here? Can anyone point to something in the DOM documentation or W3C specification that could resolve this?
Note I am specifically interested in a non-library solution (jQuery/Prototype).
Give your form an id only, and your input a name only:
<form id="myform">
<input type="text" name="foo">
Then the most standards-compliant and least problematic way to access your input element is via:
document.getElementById("myform").elements["foo"]
using .elements["foo"]
instead of just .foo
is preferable because the latter might return a property of the form named "foo" rather than a HTML element!
[1] document.forms[0].elements[0];
"No-omg-never!" comes to mind when I see this method of element access. The problem with this is that it assumes that the DOM is a normal data structure (e.g.: an array) wherein the element order is static, consistent or reliable in anyway. We know that 99.9999% of the time, that this is not the case. Reordering or input
elements within the form, adding another form
to the page before the form in question, or moving the form in question are all cases where this code breaks. Short story: this is very fragile. As soon as you add or move something, it's going to break.
[2] document.myForm.foo;
I'm with Sergey ILinsky on this:
- Access arbitrary elements by referring to their
id
attribute:document.getElementById("myform");
- Access named form elements by name, relative to their parent form element:
document.getElementById("myform").foo;
My main issue with this method is that the name
attribute is useless when applied to a form. The name is not passed to the server as part of the POST/GET and doesn't work for hash style bookmarks.
[3] document.getElementById('foo');
In my opinion, this is the most preferable method. Direct access is the most concise and clear method.
[4] document.getElementById('myForm').foo;
In my opinion, this is acceptable, but more verbose than necessary. Method #3 is preferable.
I just so happened to be watch a video from Douglas Crockford and he weighed in on this very subject. The point of interest is at -12:00. To summarize:
- Document collections (document.anchor, document.form, etc) are obsolete and irrelevant (method 1).
- The
name
attribute is used to name things, not to access them. It is for naming things like windows, input fields, and anchor tags. - "ID is the thing that you should use to uniquely identify an element so that you can get access to it. They (name and ID) used to be interchangeable, but they aren't anymore."
So there you have it. Semantically, this makes the most sense.
To access named elements placed in a form, it is a good practice to use the form
object itself.
To access an arbitrary element in the DOM tree that may on occasion be found within a form, use getElementById
and the element's id
.