...

View Full Version : Difference between name and id attributes



Mahdiem
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

sunfighter
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.

VIPStephan
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.

bullant
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.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/1998/REC-html40-19980424/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title></title>
</head>
<body>
<form name="myform" action="qwert.php" method="post">
<div>
<input type="text" name="txtUsername" >
</div>
</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum