I have pages where I've got <input name="blah" id="blah" ... where the name and id are the same.
Is it correct to have both name and id for an input element? Since I'm doing getElementById, would the id= be sufficient for all occurrences?
| SitePoint Sponsor |





I have pages where I've got <input name="blah" id="blah" ... where the name and id are the same.
Is it correct to have both name and id for an input element? Since I'm doing getElementById, would the id= be sufficient for all occurrences?


It doesn't matter if they are the same or not, but you normally need them both. The name attribute identifies the input for the form processing script, which the id associates the input with an accompanying <label> element, which has a "for=" attribute.
E.g.
The for="blah" / id="blah" associates the label with the input. That's handy for people using a screen reader (who can't "see" the relationships) and also means you can click on the Label to activate the input.Code:<label for="blah">Blah</label> <input name="blah" id="blah">
Of course, the id on the input can also be used as a hook for JS and CSS.

If you are going to use the same value for botha name and an id then you need to make sure they are on the same element. If one is on one tag and the other is on a different tag then Internet Explorer will become more confused than it normally is.
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">







Bookmarks