views:

123

answers:

5
$(textBox).focus( function() {
    $(spans).css({"background-position": "0 100%"});
});
$(textBox).blur( function() {
    $(spans).css({"background-position": "0 0"});
});

This is already short but it's either I am just too paranoid, or we could code this shorter by

$(textBox).bind('focus blur', function() { *do toggle here* });

or something else.

Any help would be greatly appreciated. =)

+4  A: 

Try this:

$( textBox ).focus( function(){...} ).blur( function() {...} );

And yes, you can also use the bind() function as you specified.

I find my version more readable.

Good luck!

Jacob Relkin
chaining... but can we not make it to something like $(textBox).bind('focus blur', function() { *do toggle here* });
Jronny
I think this is still not 'optimized' enough for our dear friend :P
o.k.w
=)... It's just that there is a toggleClass, so is there a *toggleCss* or the like?
Jronny
+2  A: 

I think there's a line between optimization and code clarity. Without any further evidence, I don't believe you'll see any major improvements as far as optimizations go.

Keeping these two separate allows for a very quick and easy scan... No logic to go through. Its just "When this happens, then do this". Simple and you're probably losing, if anything, very little.

Edit: If like you mentioned in a comment you want to toggle the class, you could

$(textBox).bind('focus blur', function() { 
var currentClass = $(this).attr('class');
var newClass = currentClass == 'gotFocus' ? 'noFocus' : 'gotFocus';
$(this).removeClass(currentClass).addClass(newClass); });
David Archer
Thanks a lot for the help...But I need to use plain css there instead of classes. =)Guess I'll stick to my code + chaining...
Jronny
you could:var currentCss = $(this).css('background-position');var newCss = currentCss == "0 100%" ? "0 0" : "0 100%";$(this).css('backgroun-position', newCss);
David Archer
@jerjer's solution is neater than mine :-)
David Archer
+2  A: 

If you want to use the same function for both the events, you need to use a way of changing the effect that can be used to turn it both ways. Something like:

$(textBox).bind('focus blur', function() { $(spans).toggleClass('over'); });

If there are a lot of elements that you want to change the effect on, you should consider using the cascading effect of CSS instead of letting jQuery change the class for all the elements, i.e. changing the class for a single parent element, and have a style that targets all the child elements that you want to affect:

#container span { background-position: 0 0; }
#container.over span { background-position: 0 100%; }

Changing the class of the parent will change the style of all affected children:

$(textBox).bind('focus blur', function() { $('#container').toggleClass('over'); });
Guffa
+1  A: 
$("#test").bind("focus blur", function (e) {
    $(spans).css({"background-position": e.type === "focus" ? "0 100%" : "0 0"});
});
jitter
+3  A: 

You can try this as well:

$(textBox).bind('focus blur', function(e) {
    var bg = (e.type=='blur') ? '0 0' : '0 100%';
    $(spans).css({"background-position": bg});

});
jerjer
salamat kababayan!
Jronny