Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between id and name attributes in HTML

What is the difference between the id and name attributes? They both seem to serve the same purpose of providing an identifier.

I would like to know (specifically with regards to HTML forms) whether or not using both is necessary or encouraged for any reasons.

like image 538
yogibear Avatar asked Sep 09 '09 04:09

yogibear


People also ask

What is the difference between name id and class in HTML?

In Html for an element ID name starts with the “#” symbol followed by a unique name assigned to it. On the other hand class assigned to an element has its name starts with “.” followed by class name. Only one ID selector can be attached to an element. Multiple class selectors can be attached to an element.

What is a name attribute in HTML?

The name attribute specifies a name for an HTML element. This name attribute can be used to reference the element in a JavaScript. For a <form> element, the name attribute is used as a reference when the data is submitted. For an <iframe> element, the name attribute can be used to target a form submission.

What is the difference between id and value in HTML?

The id attribute can be used to give an element a unique identifier that can be used in client-side scripting and styling. It has nothing to do with the functionality of the name attribute. The value attribute in a text input box specifies the initial (default) content of the input field.

What is true about id and name attributes?

Like the id attribute, the name attribute must begin with a letter and is case sensitive, but unlike the id attribute, it can be not unique. The name attribute cannot be referenced in CSS.


1 Answers

The name attribute is used when sending data in a form submission. Different controls respond differently. For example, you may have several radio buttons with different id attributes, but the same name. When submitted, there is just the one value in the response - the radio button you selected.

Of course, there's more to it than that, but it will definitely get you thinking in the right direction.

like image 167
John Fisher Avatar answered Oct 02 '22 14:10

John Fisher