I’m developing a javascript-based application.
Using id’s to identify elements is often considered a bit bad, because id’s are global and it would instantly mess up if I happen to have a collision.
What are some effective ways to avoid this? Is it ok to use use classes wherever you normally use id’s or are there drawbacks. Note that I’m not as much interested in the css side of things, just the javascript.
edit: I think I got my answer, but just so we’re clear, I wasn’t talking about duplicates in the document object. I want to build a set of re-usable components. Generally I’d access them by their id.
I’m not generating the html using the DOM, there’s an html template that’s then ‘enriched’ by javacript. The problem starts when I want to re-use a component on the same page. Namespacing won’t help here either; so even though my elements are ‘unique’ within the context of that component, they are not guaranteed to be within the entire document.
Using classes might be bad practice, but what other option is there?
use ID’s if it’s unique, use classes if it’s reused
An example of using an ID that isn’t a “bit bad”
Chances are, these are only going to be on the page once
Example of using classes
These are generic and can be used in multiple cases, even together!
<div class="floatleft bold center"></div>