Do we need to use both ID and name? Can they be the same?

In regards to the element, why do we use the ‘id’ and ‘name’ attribute when both have the same value?

My understanding is the ‘id’ attribute is required to connect with correct label element while the ‘name’ attribute is just a unique name for the input element.

The name attribute is used in the HTTP request sent by your browser to the server. The id is for CSS and JavaScript as a reference that the element with that id.

They do not have to be the same but they can be as in the case in the tutorial.

Id’s must be unique to the tag that it is inside and only used once on any given html page.

name doesn’t have to be unique, for example there are cases where you would use it many times with radio buttons.

Here’s a link to an example where the name attribute is used many times:

Also, a page with a list of attributes:

28 Likes

If ID is used in CSS and JavaScript, then why is ID used in HTML to link the input and the label?

2 Likes

The name is used to link the input to the label in HTML

name and value used for HTTP requests only. ID and class are for CSS and JS. It is more difficult to using different technologies in a project.

So the ID is not necessary unless you want to style that particularly <input> or you want to make it a target for a hyperlink. If you want to style the different <input> in groups use class instead, and if you go for maximum consistency in your <input>'s just style them using the tag name <input>. Hope that is correct.
And in the next lesson I have just been proved wrong I think. The name of the <input>'s are the same, it’s topping, whereas the id is also the one used in the label. So the id and the 'for=" " ’ seems to connect the label and the input.