Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Name vs Id attribute in HTML

Tags:

html

Are there any advantages to using <div id="here" ... instead of <div name="here" ...?

Are they both referenced to as #here?

like image 772
DarkLightA Avatar asked Dec 20 '10 08:12

DarkLightA


People also ask

What is id and name in HTML input?

name is the name that is used when the value is passed (in the URL or in the posted data). id is used to uniquely identify the element for CSS styling and JavaScript. The id can be used as an anchor too.

What is a name attribute in HTML?

Definition and Usage 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.

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 the difference between name and value in HTML?

Value = The value attribute specifies the value of an element. Name = name is only to post form data. The name definies what the name of the attribute will be as soon as the form is submitted. So if you want to read this attribute later you will find it under the "name" in the POST or GET Request.


2 Answers

Here are some differences between both:

  1. name has never been a div element attribute.

  2. name as used on the form controls (input, textarea, select, and button elements) is radically different from the id attribute on named elements. In this case, the name attribute relates to how data is labeled when sent to server, and multiple elements may share the same name.

    The id attribute on the other hand is for identifying one unique element for the purposes of scripting, styling, or addressing.

  3. The use of the name attribute on other elements than form controls was in HTML 4.01 the same as that of id, but it allowed for a wider set of characters than the id attribute and wasn't controlled in quite the same way. Because of this ambiguity the W3C decided to deprecate/remove the name attribute on those elements in favor for the unambigous id attribute in XHTML.

    This is also connected to another detail of XML however - only one attribute of any element may be of the type ID, which would not be the case if they let name stay on the element, but corrected the ambiguity problems.

  4. As the name attribute didn't work the same on those two sets of elements, it was best to remove one of them.

In short, for backwards compatibility, you should use the name and id attribute both, both set to the same value, for all elements except form controls if you use HTML 4.01 or XHTML 1.0 Transitional. If you use XHTML 1.0 Strict or later you should use only id. For form controls, you should use name for what you want the form to send to the server and for DOM 0 access, and only use id for styling, DOM1-3 access or addressing reasons

like image 54
Pratik Avatar answered Oct 17 '22 20:10

Pratik


It depends on where you are going to use them.

Usually, id of an element is unique while multiple elements can share the same name.

Id is referenced as #here, and name is referenced as [name=here].

like image 35
craftsman Avatar answered Oct 17 '22 18:10

craftsman