View Full Version : Difference between name and id attributes

04-17-2011, 03:37 PM
Hello again

I was wondering if some one could clearly explain the difference between ID and NAME attributes in html tags. And which one is used with scripting language such as JavaScript and PHP to refer to.

Thanks a lot

04-17-2011, 11:13 PM
The name attribute is used to mark an anchor or a bookmark in a page. It's used with <A> and <map> tags, but it my be on it's way out (??) The ID is a css selector use to id a unique html element.

That's probably not an official definition, but that's how I see it.

04-18-2011, 12:32 AM
The name attribute is actually deprecated for all elements other than form controls (input, select, textarea) and should therefore not be used anymore (except for the aforementioned ones). Technically, ID and name have the same purpose, they uniquely designate an element. However, unlike the ID attribute, the name attribute can’t be referenced in CSS (except with more complicated attribute selectors). PHP is using the name attribute of form controls (not the form element itself, however). With JS there are designated ways to address form controls so no need to use the name attribute. Above all, you can also address the ID with JS. For internal page anchors the ID attribute should be used, as name is deprecated.

04-18-2011, 01:16 AM
yep, agree with vipstephan.

A lot of people still use the name attribute in a form tag which is deprecated in (x)html.

For example, this html will not pass the w3c validator.

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<form name="myform" action="qwert.php" method="post">
<input type="text" name="txtUsername" >