stevenhu — 2012-05-10T18:06:40-04:00 — #1
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?
ralphm — 2012-05-10T20:36:55-04:00 — #2
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.
<label <font color='"#FF8C00"'>for="blah"</font>>Blah</label>
<input name="blah" <font color='"#FF8C00"'>id="blah"</font>>
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.
Of course, the id on the input can also be used as a hook for JS and CSS.
felgall — 2012-05-10T22:55:20-04:00 — #3
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.
ralphm — 2012-05-10T22:58:55-04:00 — #4
Is that just on older versions, or are the latest ones as dopey?
stevenhu — 2012-05-11T12:40:20-04:00 — #5
Good to know. I'm adding the id= to existing name= on form elements, and they do match each other only, and not other elements.