Hey Dave, You blew me away with your method; it's so much more elegant than the madness I was getting into. Thanks a lot! I've got a couple of questions, though: 1) Is $("#phrases").find("li") faster than $("#phrases li")?, or just better style? 2) Is there a way not to have to repeat the method that takes care of the rephrasing in the test suite? As you can see in the updated version (http://pastie.org/341151) I'm repeating the code so it work for consequent events.
Thank you so much, Nacho On Dec 17, 4:49 am, Dave Methvin <dave.meth...@gmail.com> wrote: > I think this code does what you want. Instead of trying to if/then all > the cases, you can let jQuery's chaining take care of a lot of it. > > $(document).ready(function() { > var $phrases = $("#phrases").find("li"); > function rephrase(dir) { > var pos = $phrases.index($phrases.filter(".current_phrase")); > pos = Math.min($phrases.length-1, Math.max(0, pos + dir)); > $phrases.removeClass("prev_phrase current_phrase next_phrase"); > $phrases.eq(pos-1).addClass("prev_phrase"); > $phrases.eq(pos ).addClass("current_phrase"); > $phrases.eq(pos+1).addClass("next_phrase"); > } > $("#phraseNext").click(function(){ rephrase(+1) }); > $("#phrasePrev").click(function(){ rephrase(-1) }); > > }); > > The other way to do it would be via a plugin, which is what you did > originally. However, I would make the phrase list the target of the > plugin and initialize it in the ready handler. Then have the plugin > respond to "moveNext" and "movePrev" events that the buttons would > fire.