1
$(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. =)

2
  • 1
    paranoid indeed. haha, just kidding. Commented Dec 1, 2009 at 9:01
  • 1
    Reading minds in not yet supported by jQuery, although you might see it in the near future. ;) Commented Dec 1, 2009 at 9:02

5 Answers 5

5

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!

Sign up to request clarification or add additional context in comments.

3 Comments

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

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});

});

Comments

2

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); });

3 Comments

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...
you could: var currentCss = $(this).css('background-position'); var newCss = currentCss == "0 100%" ? "0 0" : "0 100%"; $(this).css('backgroun-position', newCss);
@jerjer's solution is neater than mine :-)
2

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'); });

Comments

1
$("#test").bind("focus blur", function (e) {
    $(spans).css({"background-position": e.type === "focus" ? "0 100%" : "0 0"});
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.