Is there any reason NOT to set a tag's attributes ID and Name the same?
Example:
<input type="text" id="Label" name="Label" />
The project I am working on uses the name attribute with an SQL Database. The ID attribute is used for JavaScript.
Is there any reason NOT to set a tag's attributes ID and Name the same?
Example:
<input type="text" id="Label" name="Label" />
The project I am working on uses the name attribute with an SQL Database. The ID attribute is used for JavaScript.
Providing you follow all the rules surrounding ids and names, there is no harm (and some code management benefit as things are kept simple) in having matching id
s and name
s, but an id
must be unique but sometimes a name
must be duplicated so it isn't always possible.
For example:
<input type="radio" name="myRadio" value="a" id="myRadio_a"><label for="myRadio_a">A</label>
<input type="radio" name="myRadio" value="b" id="myRadio_b"><label for="myRadio_b">B</label>
<input type="radio" name="myRadio" value="c" id="myRadio_c"><label for="myRadio_c">C</label>
Or, for another example, if you have two similar forms on the same page (such as login and signup).
Like Quentin said, since the name and id have two purposes, sometimes it is necessary to have the same name across multiple fields, which is not allowed with ids. Another example where the name must be the same for multiple fields are with checkboxes.
In regards to using the same text for both an id and name within a single tag, it is completely legitimate to do so. I tend to do that for everything except groupings such as checkboxes or radio buttons.
I'm not sure if it's the best practice, it could confuse you but it isn't an issue, the browser sees them as two different things.