What's the difference between the name and id attributes?

Ok thank you.
Speaking of specificity, is it better to select an element using its name attribute or is it better to add a class attribute to it and then select it using a class selector

In my view the better form would be whichever needs the least amount of markup. If we can select the element by an attribute it already has then we needn’t add another attribute. Less is more when it comes to CSS. At first this might sound strange, but as you continue to develop HTML/CSS it will become a little more clear.

1 Like

Why hasn’t the W3C chosen to simply send whatever is in the id-attribute instead of adding another attribute (name)? This way, more often than not both have to be defined without much added benefit, like so …

<input id="username" name="username" type="text">

The name attribute is much older than the id attribute, and the basis of form submit. The name attributes are parsed out of the DOM and the corresponding values written with them in the request header.

username = value

where value is the attribute containing the user input.