0

Like the title says, is there anything inherently wrong with assigning a bogus class to an element just to use as a selector? For example, dynamic table rows.

It seems sloppy, and if I've learned anything, it's that just because it works, doesn't mean it's right.

Thanks!

B.Clark
  • 202
  • 1
  • 2
  • 8
  • What exactly is a "bogus class"? – Pointy Jun 06 '18 at 19:37
  • Not really. Especially if your element already has an ID. Though, you could likely use data-attributes as well if you don't want your class attribute filled up. – Adam Jun 06 '18 at 19:38
  • @Adam how does a class attribute "fill up"? – Pointy Jun 06 '18 at 19:38
  • 1
    @Pointy - A class that serves no purpose other than being selectable. No CSS tied to it. – B.Clark Jun 06 '18 at 19:39
  • @Pointy, would you want an element who's class attribute has 10-20 different classes? Me either. https://stackoverflow.com/questions/1496096/is-there-a-limit-to-the-length-of-html-attributes/1496165#1496165 – Adam Jun 06 '18 at 19:40
  • Nothing wrong but I would use a data attribute so you don't need to now keep track if your "bogus" class actually matches the name of a "legitimate" class that is used for styling or as a JS handle, etc. – CSSBurner Jun 06 '18 at 19:40
  • 2
    I'll stop trying to be irritatingly socratic. A class attribute can contain an arbitrary number of classes. HTML does not stipulate what the class values should be used for. CSS interpretation does not care whether classes that happen to be on HTML elements are referenced by selectors. Classes are for use by *anything* examining a DOM. There really is not any such thing as a "bogus class". – Pointy Jun 06 '18 at 19:40
  • @Adam - I'd considered data attributes. Is there any benefit to using either over the other? – B.Clark Jun 06 '18 at 19:40
  • As Pointy said about, classes are for anything - but data attribute can be just as useful in your JS selectors. – Adam Jun 06 '18 at 19:41
  • 2
    Nothing anywhere says that classes are only for CSS. The specifications for CSS and HTML are completely separate and really have nothing to do with each other, or not much. – Pointy Jun 06 '18 at 19:41
  • @DavidPartyka Very fair point. In this case, the HTML isn't initially loaded in the DOM. I'm building a table from returned API data called via AJAX. The table html is passed back to my js library and appended to the table body. I solved that using a class through .on()'s data parameter. Can you do the same with a data-attribute? – B.Clark Jun 06 '18 at 19:45
  • Yes, you can solve the same problem with a data attribute. Post some code and we can give you an example (of your returned data and your selector). – Adam Jun 06 '18 at 19:46
  • 1
    @B.Clark There's probably performance reasons to use class instead of data. I expect most browser automatically index classes to make searching them quick, but searching for data attributes requires a full scan. – Barmar Jun 06 '18 at 19:51
  • Classes are generally considered the normal way to do this. Look at libraries like jQuery UI and Bootstrap, they make heavy use of classes to attach semantic data to elements, not just for CSS styling (although they do that quite a bit, too). – Barmar Jun 06 '18 at 19:55
  • Remove the belief that a html class *must* match a css class, and then the issue becomes mute. – Taplar Jun 06 '18 at 20:10

0 Answers0