Q:

JQuery syntax for multiple operations

I have a list if <span> elements, which I need to filter and apply multiple operations (text replacement and add a class conditionally). I have the following working code:
$('span').filter(function() {
     return $(this).text().toLowerCase()==phrase;
}).text(translation);

$('span').filter(function() { 
     return $(this).hasClass('translatable') && $(this).text().toLowerCase()==phrase;
}).addClass(translated);

This works, but is clearly going through all the <span>s twice. Is it possible to combine them into one iteration, something on the lines of:
$('span').filter(function() { 
     return $(this).text().toLowerCase()==phrase;
}).function() {
     $(this).text(translation);
     if($(this).hasClass('translatable'))
         this.addClass("translated")
}

A:

Given that the phrase test is the same for both of your filters I'd probably approach it like this:
$('span').filter(function() {
     return $(this).text().toLowerCase()==phrase;
})
.text(translation)
.filter('.translatable')
.addClass('translated');

That is, first find the spans containing that phrase and set their text, then filter that set to get the subset that has the translatable class and add the 'translated' class just to those.

