I have a bit of jQuery that looks like this:

$("#long-selector-1, #long-selector-2, #long-selector-3").filter(":visible").fadeOut(400, function() {
    otherDivs.show();
});

I have two competing issues here:

  1. The line is over 80 characters, so I'd like to break it up
  2. Chaining Jquery statements across multiple lines:

    $("#long-selector-1, #long-selector-2, #long-selector-3")
        .filter(":visible")
        .fadeOut(400, function() {
        otherDivs.show();
    });
    

doesn't pass JSHint because of the possibility of automatic semicolon insertion.

Is there a best practice to follow here?

share|improve this question

3 Answers

I always break chains of functions, one per line after the first function call. So this:

var $blah = $('#blah').hello('hola').goodbye('adios').tanj('no hay justicia');

becomes:

var $blah2 = $('#blah').hello('hola')
                       .goodbye('adios')
                       .tanj('no hay justicia');

BTW, I ran that exact code through JSHint (online at jshint.com) and didn't receive any errors... Just for giggles I ran it through the stricter JSLint and didn't get any errors... Not sure what switch you're toggling.

share|improve this answer

Long lines are to be avoided, but whatever js error you received before it wasn't caused by splitting chained calls onto multiple lines.

That said - be careful with whitespace.

$("#long-selector-1, #long-selector-2, #long-selector-3")
    .filter(":visible")
    .fadeOut(400, function() {
        otherDivs.show();
    });

The above is slightly easier to read purely because the fadeout function is indented such that otherDivs.show doesn't look like it's being applied to the selector.

If you use a selector which changes the scope of the call, inconsistent whitespace can make it hard to read the code at a glance.

e.g.

$('.foo')
    .highlight()
    .children()
    .hide();

makes it look like it's going to hide all the .foo of course it won't.

$('.foo')
    .highlight()
    .children()
        .hide();

This makes it a little more obvious that the hide function applies to whatever is returned by .children()

share|improve this answer

You can pull the selector out of the first call, but honestly the better question is why do you still care about the (extremely arbitrary) 80 character limit for lines? This was only important when all programmers used some sort of terminal. I'm 100% certain that you can display more than 80 characters in your IDE with some font size changes.

This is the nature of the beast when you are working with chained function calls in jQuery.

share|improve this answer
The 80 character limit for lines is a coding guideline. Whether you like it or not, it is not a matter of whether the screen can display it or not. Most editors can wrap lines too. It's just a coding guideline. – Florian Margaine May 16 '12 at 20:05
It was originally a factor of screen size, and I vote that it is archaic. If your organization's style guide specifically requires 80 characters, I would encourage you to increase it. I am happy to cite several examples to back this up. – Sam May 16 '12 at 20:06
When the lines are 80 characters or less, I can fit 3 files side-by-side on my screen. There are other good reasons for short lines. – Kevin Burke May 16 '12 at 20:10
Drupal coding standards require this 80 characters limit. It's actually still a matter of screen size btw, nowadays we split the screen, having this limit allows us to still get easily-read code. Btw, coding guidelines are entirely subjective, and debating about this would be pointless. – Florian Margaine May 16 '12 at 20:10
coding guideline(s) are not rules. They are exactly what they are trying to be. Guidelines... nothing more... no golden rule here, do whatever is readable and maintainable to you. If your co-worker decides his VI doesn't like you 120 char line... well he can change his editor. – rlemon May 16 '12 at 20:10
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.