4

I'm curious as to how other people indent/break long jQuery chains, as I can never decide what is more readable (particularly when using .end() to "close" a block of methods e.g.

$(this).not(":has(.pointer)").append(pointerHtml)
                .end()
            .closest("li").toggleClass("selected")
            .siblings().removeClass("selected")
            .andSelf().removeClass("pre-selected")
                .end().end()
            .filter(".selected").prev().addClass("pre-selected");

How would YOU format this?

wheresrhys
  • 22,558
  • 19
  • 94
  • 162
  • 2
    I like this question, but it's just inherently subjective. It might make a good CW discussion, or else perhaps a good one on Programmers. – Pointy Apr 14 '11 at 16:06
  • I was going to ask if it was still possible to tag a question as "opinion" on here as I coudln't see how – wheresrhys Apr 14 '11 at 16:10
  • Wow - that was a swift closure. Point taken that this is a subjective question, but to close it so rapidly because it's *likely* to end up in an almighty spat seems a bit too pessimistic to me. Most questions about good practice in writing code (which is what this question, in essence, is about) are subjective to some degree, and it's a shame that the stackoverflow community won't be able to voice their opinions on this. (*edit* Reposted on Programmers as soon as was suggested - question views so far: 0) – wheresrhys Apr 14 '11 at 16:22
  • see http://stackoverflow.com/q/1286829/277335 – Andy Jul 05 '13 at 13:28

1 Answers1

-1
$(this).not(":has(.pointer)")
           .append(pointerHtml)
           .end()
           .closest("li")
           .toggleClass("selected")
           .siblings()
           .removeClass("selected")
           .andSelf()
           .removeClass("pre-selected")
           .end()
           .end()
           .filter(".selected")
           .prev()
           .addClass("pre-selected");
Adam
  • 43,763
  • 16
  • 104
  • 144