1

Is using the class attribute only for javascript purposes generally accepted or should it be restricted to CSS? I know it probably doesn't matter; but I'm using the class attribute to find elements with JavaScript, but there is no equivalent CSS class. Visual studio warns me of this, leading to think it's not a good practice. Is this generally accepted as OK or frowned upon? I realize this a bit subjective, but I think the question is valid.

3
  • 1
    it's perfectly ok, but if they are single elements, use id instead Commented Aug 10, 2011 at 23:07
  • 1
    Yes it's ok, and don't get too caught up in what is "generally accepted" or not by the community. Much of it is arbitrary, impractical and/or idealistic rules that will only make your life more difficult. Commented Aug 10, 2011 at 23:11
  • @Rob -- I have to disagree with you there. Things are or are not "generally accepted" for a reason. If you know the reason, if you understand it thoroughly and then reject it, that's one thing; just ignoring what other people have learned over the last five decades is foolishness. Just don't get me started on the Javascript with statement... Commented Aug 10, 2011 at 23:18

4 Answers 4

3

Visual Studio is being needlessly pedantic. It's common practice to add classes for access by JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

1

Throughout our company's code, we use CSS classes to help with element selection all the time. I wouldn't worry about the Visual Studio warnings; a lot of that stuff was designed before the jQuery behemoth reared its head.

4 Comments

What has jQuery got to do with it? People were writing getElementsByClassName functions (and I think there was built-in support in some browsers) long before jQuery existed.
That wasn't added to Firefox until version 3. I don't think it was really a viable cross-browser option "long before jQuery existed."
Here's an interesting article on that native method: quirksmode.org/blog/archives/2008/05/getelementsbycl.html
Host support for getElementsByClassName must be feature detected even today. My point was really that people were writing such functions, and the class attribute was being used for varions purposes (it was never intended to be only for CSS) long before jQuery was released. It was discussed in CLJ in 2000, I'm sure there are earlier references.
1

It is perfectly ok if you're using jQuery but if you are using plain JavaScript then use id since you can get the element by calling getElementById() and there is no simple equivalent for class.

1 Comment

Except perhaps for getElementsByClassName. :-)
1

I can't speak for the general community, but I've thought about this a lot and my own feelings are that it is acceptable if the elements represent some logical class, some things that could reasonably be styled the same way, even if they aren't currently. Just tagging a bunch of otherwise unrelated things with the same class so you can grab them with one call feels like cheating to me.

2 Comments

If you need to "grab them with one call", doesn't that represent a logical grouping and hence a class?
@RobG -- a reasonable question, one to which I would answer "usually". Or at least, the need to grab them with one call suggests a logical grouping. Sometimes, it's just crazy product-managers lumping a bunch of stuff together.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.