Today I was happy to learn the jQuery .one() event. Here's a classic example why you might want to use this. Imagine a form where you put sample text inside a field. For instance, in the telephone number field, you might put something like (000) 000-0000 (please forgive me if you're not in the USA, but you get the idea). And then you can grey it out with a style on that field. So, instead of #333 (near black), you can use #999 (mid-grey).
Okay, fine, but then you may want to make it such that when the field receives focus for the first time, it returns the font color in that field to #333 and clears the value out, and not make that focus function run any more than once. What's the fix?
Well, you could have implemented it with a boolean global var indicating whether this is the first time or not, and a .focus() function. You could probably think of a couple other ways. But the quick fix is this:
Code: Select all
$('#fldPhone').one('focus',function() {
$('#fldPhone').val('').css('color','#333');
});
Of course, there's a lot more you can do with jQuery, and things like jQuery UI effects and drag and drop sortables are cool and easily implemented, but this one() routine was something new I learned today.