2

I've went into argument with my friend, who says that for building applications, using jquery .css function, is more flexible, readable (since attributes sit in the same file), and overall better versus the addClass. For me addClass is much better, since it allows to keep code clean, and keep the logic of functions, outside the style propereties.

What do you think, and if you agree - how can i convince my friend to start using addClass? Or maybe im wrong all along?

2 Answers 2

2

In many projects, the fine details of the look, formatting and visual cross browser compatibility of the site are done by a different person than the one who writes the javascript code. Thus, it's a mess to have the CSS that controls the look of the site intertwined with the javascript of the site all in the same file(s). You have non-JS programmers trying to modify javascript in order to modify the look of the site. This is not a good thing.

However, if you add/remove classes via javascript, then all the actual CSS that corresponds to those classes stays in CSS files where it can be freely worked on without modifying the javascript files. This is a good thing.

Obviously the javascript and CSS are not 100% independent since they often have to do coordinated things, but if they are kept separate, it lets at least 90% of the maintenance be separate rather than 0%.

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

Comments

1

addClass allows a developer to enumerate all of their style information in a stylesheet. One way to demonstrate the value of this approach is to consider the problem of theming. If all of your styles are tied up in Javascript, a new theme would require a bunch of adjustments to options (at best) or a separate set of JS files (at worst).

The one situation where it makes more sense to me to use the css function is in animating position and opacity attributes that are required to perform specific tasks (e.g., sliding a <div> into place) rather than simply styling elements.

Comments

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.