0

I am building an app that could become pretty heavy and I read this answer about browser's way to parse css. As I am using selectors like $('#questionnaire .answer[data-value="value"]') pretty often I was wondering if these selectors are indexed as the IDs and classes are for all I know.

Thanks in advance.

Community
  • 1
  • 1
Johnride
  • 8,476
  • 5
  • 29
  • 39
  • A possible solution would be to add a class to `$('.answer[data-value="value"]')` and use the new class as the selector. For example, `$('.answer[data-value="value"]').addClass('.foo')` then use `$('.foo')`. – Dom Mar 13 '13 at 16:55
  • 1
    Switching from `$('context selector')` to `$('selector', 'context')` will increase performance. (You seem to not be aware of that.) If you context is fairly specific, you *don't* have to worry about selector performance. – Šime Vidas Mar 13 '13 at 17:05
  • These are called attribute selectors by the way - I don't think browsers index them like they do IDs and classes considering the nature of HTML attributes in general. I doubt jQuery optimizes them much either. – BoltClock Mar 13 '13 at 17:53
  • @Dom I don't re-use the same object often but I go through many objects with different values (value is concatenated using a js var in the selector) – Johnride Mar 13 '13 at 18:04
  • This is irritating if true, since recently I've extended the conventional wisdom of "don't user IDs as CSS selectors (because you're converging concerns with native behaviour)" with "don't use classes as JS hooks (use custom attributes instead — because classes are for appearance and IDs are for native behaviour)". I've often been dismayed by people hooking CSS and JS to classes, then re-arranging their instances in markup for styling reasons. The whole [data-*] thing was my ticket out of that nonsense! – Barney Mar 13 '13 at 18:09
  • ŠimeVidas Vidas that's what can help me the most for now, thanks. BoltClock thanks for the vocabulary tip, I think the same about indexing but I hope someone will have a stated answer... – Johnride Mar 13 '13 at 18:25
  • @Barney What is irritating if true? If I get you right you say that the use of data-answer is the way to go in my case or in any case where one wants to store data in an attribute? The thing I dont get is the 'Dont use IDs as CSS selectors' I thought that it was the best css selector to identify a unique element since it is indexed and supposedly O(1). – Johnride Mar 13 '13 at 18:31

0 Answers0