Reputation: 747
I have to use form elements generated with ID's in two parts of the site.
the problem is ID's are being doubled and W3C shows errors
is there a way to ommit that problem?
Upvotes: 0
Views: 512
Reputation: 15945
Don't use id's if not completely required. Try to get them using names and classes.
Upvotes: 0
Reputation: 317119
Please refer to the official W3C specs for the id and class attributes. Quoting:
Attribute definitions
id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.
class = cdata-list [CS]
This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.
As you can see, the ID may not be the same. The validator will not accept anything else. It's not negotiable. If you want to create valid markup, you change the IDs to be unique. Simple as that.
The only way around this is to use your own DTD to validate against that doesn't require ID attributes to be unique. Of course, it wont be HTML then but MiojamoML and you have to serve it as such and you will likely run into a number of quirks in the various browsers then and it's not feasible to do this, but that's how it is. At least if you want to do it properly.
Note that if your aim is to have named identifiers on the form elements for form submission, then you should use the input name attribute
instead. This attribute does not have to be unique.
Upvotes: 0
Reputation: 71170
Is there a particular reason the same ID needs to be generated more than once? Can you not simply, if it is needed in some way, replicate a specific part of the ID for both, and add an extra unique identifier? The relevant part can then be extracted later.
So for example, prepend the id with the name of the page/form the element is on/in.
That said, it sounds like there is an issue with methodology and approach here, is there any specific reason you cant have different ids?
See here for more info: http://www.w3.org/TR/html4/struct/global.html#h-7.5.2
Upvotes: 1