tags:

views:

178

answers:

7

First of all I alwyas use one #ID. just asking this question to know deep reason behind it.

Is it only a matter of W3C validation? or more than that.

Will i face any practical problem or it's just a logic for validation?

see this example http://jsbin.com/aniqi it's working on all browsers. just validation giving error

update : I think all browser supports more than one #ID so it's just a recommendation but supported by browsers.

only validation give error : ID is already defined.

and if we use javascript to select #ID, other wise it's not a problem

Update 2: What should be the answer if some web design students of mine asking to me about "Why css #ID should be used once in a page. student doesn't know about javascript yet. What reasons i can give to not to use #ID more than one, while it's supported by all browsers?

Update 3: I found some useful info on W3C site

<P id="myparagraph"> This is a uniquely named paragraph.</P>
<P id="yourparagraph"> This is also a uniquely named paragraph.</P>

The id attribute has several roles in HTML:

The id attribute assigns a unique identifier to an element (which may be verified by an SGML parser). For example, the following paragraphs are distinguished by their id values:

* As a style sheet selector.
* As a target anchor for hypertext links.
* As a means to reference a particular element from a script.
* As the name of a declared OBJECT element.
* For general purpose processing by user agents (e.g. for identifying fields when extracting data from HTML pages into a database, translating HTML documents into other formats, etc.).
+2  A: 

document.getElementById() won't work reliably.

Annie
so it's only effect if i use javascript and try to select a #ID
metal-gear-solid
Yes, but you need to be careful about what JavaScript libraries you're using--they could be selecting by ID under the hood without you realizing it.
Annie
+1  A: 

I've had experiences where certain JavaScript code didn't work as expected because two elements had the same ID. I think getElementById just gets the first element it comes to that has the particular ID.

Brandon Montgomery
so i would not face any problem if i do not use javascript
metal-gear-solid
True, but it's easy enough to use different ID's isn't it. I would STRONGLY recommend NOT having duplicate ID's.
Brandon Montgomery
@Brad- i know it best practice. and i do. i just wanted to know is it really implemented in browser or just a recommendation
metal-gear-solid
A: 

At a minimum, Javascript code which uses getElementById() will return only one of the matching elements - which one will be undefined, and hence subject to cross-browser differences.

Ed Schembor
what if i not use javascript
metal-gear-solid
+8  A: 

The result is undefined.

Apart from what the rest said about getElementById:

  • using #foo in CSS may apply to all elements with id="foo", or to the first, or to the last, depending on how the browser is implemented.
  • using page.html#foo in a URL may scroll to one of the elements, or to the other, or to neither.

edit: either way, you should teach your students to do things right even if doing it wrong doesn't seem to have any immediate bad consequences... ;)

Nicolás
+1 for mentioning that it can break CSS and anchors as well.
Aaronaught
@Nicolas - u r right but what reason should be given for now?
metal-gear-solid
+1 for "do things right even if doing it wrong..." note
Ed Schembor
+1  A: 

"Why I'm saying css #ID should be used once in a page (to my students)?"

"Imagine the trouble it would cause if lots of countries had the same name. ID is identity. Many countries can be an island, or tropical, or as small as a carpet but think of the trouble it would cause if 50 countries were called San Marino."

Gazzer
+1  A: 

I understand why you're asking the question. Tell your students that's it's the potential problems that they will face if they use ID incorrectly. Or, other coders that want to use/alter/correct your code if you write it incorrectly.

In other words, try not to leave slop in your wake when you build your page because it's good practice not to leave slop for other coders. And, if you come back to that page and you want to make it dynamic, you'll have slop when you're done, and you won't know why.

tahdhaze09
so we can say it's must only for xhtml validation and it would be much better to use once for future dynamic use.
metal-gear-solid
Also for the sake of semantics. Your layout will have elements that will not repeat on a single page (header, footer, container, sidebar, main menu, etc.). Using those elements multiple times on the page will make for a complete mess in having another coder understand it, not to mention it breaking the layout.
tahdhaze09
ok so i would say this to students "You should only use an ID name once in any XHTML or HTML document.Browsers do not enforce this rule but it is a basic rule of HTML/XHTML. Duplicate ID tags will cause your page to fail validation, and can have negative effects when using JavaScript with them"
metal-gear-solid
+1  A: 

This post is just to add some more points to this interesting discussion ..

Its true that .. Id must have been unique and usage of 'duplicate IDs' must have been depreciated by browsers itself ..
But may be because these browsers want to make it more coder-friendly, don't seriously consider the "id" matter ..
So apart from javascript this mistake doesn't matter much to us too ...
(Depends on the version of the different browsers) Even CSS has no problem with it .. (apart from validation errors) check this out ..

<head>
<style type="text/css">
  #Button{color:red;}
</style>
</head>
<body>
<form>
<input id="Button" type="button" value="Click me"/>
<input id="Button" type="text" value="Type Here"/>
</form>
</body>

But I assure that in-case of javascript usage it is prone to hell of bugs .. Once I had faced this problem .. where I had used same name for ID as well as NAME .. !

:-)

infant programmer
"Even CSS has no problem with it" in your particular version of your particular browser.
Nicolás
@Nicolas:: ohk .. clarified ..
infant programmer