1

I've come across some HTML that uses classes without any real CSS attached to them rather than id's just to group/identify them with jquery selectors:

html:

<div class=someClass>
</div>

<div class=someClass>
</div>

<div class=anotherClass>
</div>

CSS:

(nothing, just an empty file)

javascript:

$(".someClass").each( ... )

I think I know why they do this: because html elements can only have one id and it must unique. But it feels as if it's "a dirty workaround" trick to use classes for identification rather than style. Is this a common trick that is generally approved, or is it frowned upon and do better alternatives exist?

user1884155
  • 3,616
  • 4
  • 55
  • 108
  • 3
    possible duplicate of [Can I use non existing CSS classes?](http://stackoverflow.com/questions/18701670/can-i-use-non-existing-css-classes) – BoltClock Oct 24 '13 at 08:24
  • Is this not what classes are for? – Turnip Oct 24 '13 at 08:24
  • 1
    The only issue with selecting classes like that is noticeably slower than an id, especially in older browsers. – Johan Oct 24 '13 at 08:28
  • You could also use the HTML 5 data- attribute, if you think this is a 'dirty' workaround? – nkmol Oct 24 '13 at 08:30

2 Answers2

3

It is perfectly fine to locate DOM objects in this manner. Both classes and IDs can be used for DOM element manipulation. It's not a dirty workaround.

Nzall
  • 3,439
  • 5
  • 29
  • 59
0

I think it´s a common trick, there is not problem to use it

user2891084
  • 111
  • 4