25

Lately I found myself using jQuery and JavaScript a lot, often to do the same things that I did before using CSS.

For example, I alternate table rows color or create buttons and links hover effects using JavaScript/jQuery. Is this acceptable? Or should I keep using CSS for these kinds of things?

So the real question is: When I'm using too much jQuery? How can I understand when I'm crossing the line?

Andy E
  • 338,112
  • 86
  • 474
  • 445
Andrea
  • 320
  • 4
  • 10

8 Answers8

89

You're crossing the line if you're using jQuery to do things that can be done easily without jQuery. jQuery's purpose is to make life easier, but it shouldn't be at the expense of compatibility or usability.

jQuery most certainly shouldn't be used as a replacement for CSS -- think of the users who have JavaScript disabled.

I know this image is overused, but someone had to throw it in here:

Image Credit - bobince.

Mario Trucco
  • 1,933
  • 3
  • 33
  • 45
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • 32
    hahaha, this is the first time i've seen that image. it's pure gold – Patricia May 13 '10 at 12:55
  • 6
    -1 for having the audacity to mock the great and powerful jQuery! – Cerin May 13 '10 at 13:33
  • 5
    Correction to previous comment, I didn't notice the "Related" section on the right was pure gold as well "Where are my legs?" is the new winner. – Nick Craver May 13 '10 at 13:41
  • I bet suicide rates among web-devs would decrease if JS would be entirely replaced by *any* JS framework. – sibidiba May 13 '10 at 13:43
  • 5
    @sibidiba - In jQuery's (or any framework's) defense, if the browsers didn't implement things in such different ways to begin with, they wouldn't have been created...or at least wouldn't see the usage they currently do. Yes, I'm looking at *you* Internet Explorer team. – Nick Craver May 13 '10 at 13:47
  • 1
    @Chris S: lol. @Nick Craver: those related questions still crack me up when I see them. – Andy E May 13 '10 at 13:55
7

I don't think there is a "line" here, I think there are some straightforward things and some grey areas there you have to balance what you want. Advanced features, performance, compatibility, think of these are a triangle, it's hard to do all 3 as best as possible at the same time.

If CSS can do it, of course do it in CSS. If it can't be done in CSS use jQuery, but do't use jQuery where the overhead isn't needed, e.g. $(this).attr("id") can usually be this.id, many things are available strictly on the DOM and still in a cross-browser way.

When are you using it too much? When it's not needed, sometimes you need jQuery for cross-browser CCS3 selectors, sometimes you're using a CSS selector that's already available put it in the stylesheet. There are a hundred other examples, but if you can get by in a cross-browser clean way without it, then there's no need, things like fading aren't trivially done. If you need to include jQuery at all, there's no reason not to use .fadeIn() once you have (the code's been included, why duplicate it?)

JavaScript vs No JavaScript

As said in comments here your site should offer all the basic functionality without javascript, this usually isn't a problem, e.g. capturing a click and loading the content via AJAX...if you don't capture it they do a full page reload, this is an easy fallback to the standard behavior. However, all the "bells and whistles"? This is opinionated for sure, but I don't think you should bend over backwards to offer all the functionality without JavaScript. The user turned it off, they don't get the fancy stuff, that's fine...look at SO as an example, disable javascript disables a lot of non-essential features, you can browse around just fine, but commenting, voting, mainly actions aren't necessarily available without JavaScript.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Exactly: there is no "line" once you bring in jQuery. It's like asking "When do I have too much HTML?" You can have inefficient code but that's not really related to the library you are using. If your code is inefficient you should make it more efficient. That might mean adding MORE jQuery and removing some HTML/CSS. Or it might mean removing some JS and adding more CSS. Depends on what's inefficient about it. – Mr. Shiny and New 安宇 May 13 '10 at 13:33
  • StackOverflow has many examples of bad practises - if I wanted a compact interface to SO, I'd need to implement via a browser to make it fully usable, rather than a simpler, lightweight HTTP+DOM based application. It would be a better site if these things didn't *require* JS to be parsed. – Peter Boughton May 13 '10 at 13:42
  • For styling your site, and really is not line. But if you are dependent on reading JSON for data, and the only way that you are reading it is through Jquery, then you have a problem. Implementation should be considered, and lines can be crossed there. – Omnia9 May 13 '10 at 17:55
  • Can you explain the difference between these too? "$(this).attr("id") can usually be this.id and why you say usually." I also am wondering what happens if you need to set the id? can you go this.id = "newId"? If so what happens if your doing chaining is it still worth to do this separately and chain the rest? – chobo2 May 17 '10 at 14:44
  • @chobo2 - I say "usually" because sometimes you want an empty string vs undefined for another operation. You can change the Id in a chain, as long as you're operating off the current chain you're fine. The `$(#IdSelector)` selected the **elements** that matched when it was run, that set of elements is what's used in the chain, the fact they may change and not match the original selector doesn't matter for the life of the chain, when you use a selector again, then it matters :) – Nick Craver May 17 '10 at 14:49
6

If you turn off java script on your browser and your site/application does not run or look functionally with out it, then you have a problem.

JS is great, but it should never stop a user from using something you have built, IF it is disabled.

Omnia9
  • 1,563
  • 4
  • 14
  • 39
  • 8
    I only partially agree with this, if it's extremely complicated or a lot more effort to make it work without javascript...well then you don't get that feature. E.g. try turning javascript off here then voting, or replying to this comment...some things just aren't *reasonable* to do without javascript, and it was the user's choice to turn it off. – Nick Craver May 13 '10 at 12:50
  • 1
    A website/app should be fully usable without JavaScript. JS is for nice features to make things easier and more convenient, but functionality should work without JS. – Peter Boughton May 13 '10 at 12:55
  • 1
    Agreed, there are just some things that are easier to do with javascript. However, there can be restrictions on how much javascript is used. (i.e government related sites/applications) In that case CSS can still provide means for most things in javascript. As a side note, keep in mind that CSS has its problems too. Different browsers interpret CSS differently. – Omnia9 May 13 '10 at 12:56
  • 13
    @Peter - I *strongly* disagree with that "fully", basic functionality **yes, absolutely**...bells and whistles? **no**. JavaScript is a **standard part of every browser used today** (with a *very*, **very** small part of 1% exception), why should I have to completely ignore that it's there? If you unplug your mouse because you don't like it, that's fine, it's not my responsibility to make the application work **100%** without it...how is turning off javascript any different? – Nick Craver May 13 '10 at 12:59
  • Don't make up statistics, and don't forget that not just browsers use the web. Oddly, you're not *actually* strongly disagreeing: - All *functionality* should work without JavaScript; - JS for bells and whistles - features that make an app work *better* - that's not functionality. Yes, most applications *should* work entirely without a mouse - or any other pointing device. Don't assume it's been unplugged; there may be reasons why a user can't or doesn't want to use it. Likewise with JavaScript; it's *not* always a case of "the user turned it off, that's their choice". – Peter Boughton May 13 '10 at 13:13
  • 1
    Like I said it depends. When you have to account for Accessibility software, and you have to allow for a certain version to work with your site, your site should work if the js if turned off. May sites out there would fail an accessibility test. As a developer in a 'real world' group you need to make sure that your user base can access it. It is the responsibility of the developer to know their audience. – Omnia9 May 13 '10 at 13:14
  • 2
    @Peter - You can find numbers yourself: http://stackoverflow.com/questions/121108/how-many-people-disable-javascript Of course it depends on your audience as to what numbers matter to you, but that number doesn't come out of nowhere. Also, you have a very strange definition of functionality, since most would define that as "what something does". It's all "functionality", whether's it's *essential* functionality (e.g. navigation), that's the point. – Nick Craver May 13 '10 at 13:27
  • 6
    I find it a little arrogant for people to say that all functionality must work with JS turned off. Thanks for telling me how to optimize my engineering resources, so that I can cater to the two guys in their basements who always use the NoScript extension with no whitelist, and the one guy with a mobile phone browser from 2001. – Mr. Shiny and New 安宇 May 13 '10 at 13:29
  • Mr Shiny, nobody has said "must" but yourself. If you want to make your application the best, you shouldn't restrict your audience to JS clients. – Peter Boughton May 13 '10 at 13:32
  • Nick, you've picked (a typo of) the 1.4% of EU respondents to support your point; not sure that's valid for a general *"very, very small part of 1% exception"* statement. As for functionality - yes, "what something does" - but I see 'something' as the application, not the application's graphical interface. Hopefully that's a clear distinction? – Peter Boughton May 13 '10 at 13:39
  • @Peter - That was from 08, you think *fewer* browsers support it now? I would wager the opposite, though as I said some things navigation and such are still important, for google if nothing else else. When you're talking about the web, everything is done via the interface...I'm not sure how you can distinguish the two, if you can't do it via the interface, what it does won't really matter :) – Nick Craver May 13 '10 at 13:43
  • This was true in 2000. We are in 2010. It's like IE6 support: look at your audience, do *they* need it? If 99.9% of you visitors have JS on, and it is not an explicit requirement, then why care? It is nice, when your site works without JS, but does it have to? It is better to decide for each functionality in question: logging in is crucial, but for example WYSIWYG editing in HTML is impossible without JS. Or do you still think of ppl using console based browsers when designing a site?! Technology moves on, go with it. – sibidiba May 13 '10 at 17:47
  • 1
    It depends on who you work for. Where I work there are rules for developing static sites and web applications. JS can be used, but cannot be the primary source. There is a list of third party software that we need to be compliant with. If any one of those fails, we have to revisit the section. Stats are great, but when a governing body says you 'must' to this, you have it. @sibidiba: your point is valid, but then there are COTS (sharepoint etc) that can provide a means for those features. You develop as you are told, given your environment. – Omnia9 May 13 '10 at 18:03
  • 1
    Mr Shiny portrays NoScript users as a an insignificant minority (two guys) who have no live (in the basement). That is unfair and biased. Actually, NoScript is one of the most popular addons for Firefox. Try it! https://addons.mozilla.org/en-US/firefox/browse/type:1/cat:all?sort=popular Internet Explorer has various configurations, too. Many times, website requirements can be satisfied without javascript, but are coded in such a way that the site won't work when JS is turned off. PubMed, Yelp, dl.ed.gov. – Garrett May 14 '10 at 02:41
4

If it's something that is easily do-able in CSS, then ditch jQuery and do it in CSS. That way you don't have to depend on javascript execution for the look/feel of your application.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

You use too much jQuery if you could set one class attribute instead of a lot style attributes. For example:

/** Select 400 rows and change the background colour **/
$('#table tr').css('backgroundColor', 'blue');

Instead of:

/** jQuery **/
$('#table').addClass('blueRows');

/** CSS **/
#table tr.blueRows {
    background-color: blue;
}

To avoid jQuery styling, you could also set a class to the body so it's easier to style with CSS for Javascript-enabled browsers:

/** jQuery **/
$(document).addClass('JS-enabled');

/** CSS **/
body #table tr{
    background: #FFF;
}

body.JS-enabled #table tr {
    background: blue;
}
Harmen
  • 22,092
  • 4
  • 54
  • 76
0

jQuery most often gets applied after the document has been loaded. I guess that if you can achieve the same thing with plain CSS, CSS is the way to go. Less load on the browser, and if someone doesn't have jQuery enabled at least there's still (some) style because of the CSS.

Alec
  • 9,000
  • 9
  • 39
  • 43
0

Ok, don't mark me as a troll...

If your web-app wont work in an environment that doesn't have JavaScript enabled or isn't compatible with JQuery, then just go with whatever is easiest for you to manage. There is no benefit to having visual support for an application if it doesn't actually work otherwise at all.

Tho if you want to make it work later without JavaScript support, then you should prob try to use css. But if you don't plan for no-JavaScript support, and it works, go with whatever is easiest for you

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
Kurru
  • 14,180
  • 18
  • 64
  • 84
0

For example, I alternate table rows color or create buttons and links hover effects using JavaScript/jQuery. Is this acceptable? Or should I keep using CSS for these kind of things?

Really, it depends on your browser support. You can do zebra-striping on tables really simply with this code:

table.classname tr:nth-child(even) td {
  background-color: #ddd;
}

But this doesn't work in Internet Explorer at all (although it should in the upcoming version 9). So if you need everything to look the same cross-browser, use jQuery instead.

For link hover effects, assuming you mean colour changes, etc. and not fancy animation, definitely use CSS since this is supported everywhere.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DisgruntledGoat
  • 70,219
  • 68
  • 205
  • 290