javascript - Avoiding ids in html elements -


i'm developing javascript-based application.

using id's identify elements considered bit bad, because id's global , instantly mess if happen have collision.

what effective ways avoid this? ok use use classes wherever use id's or there drawbacks. note i'm not interested in css side of things, javascript.

edit: think got answer, we're clear, wasn't talking duplicates in document object. want build set of re-usable components. i'd access them id.

i'm not generating html using dom, there's html template that's 'enriched' javacript. problem starts when want re-use component on same page. namespacing won't here either; though elements 'unique' within context of component, not guaranteed within entire document.

using classes might bad practice, other option there?

use id's if it's unique, use classes if it's reused

an example of using id isn't "bit bad"

<div id="header"></div> <div id="content"></div> <div id="footer"></div> 

chances are, these going on page once

example of using classes

<div class="floatleft"></div>  <div class="bold"></div>  <div class="center"></div> 

these generic , can used in multiple cases, together! <div class="floatleft bold center"></div>


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -