Rowan Lewis schrieb:
> Hey,
> 
> I just wrote a short article covering 5 things I've found JQuery useful for.
> They are just beginners tips:
> http://pixelcarnage.net/articles/development/5-quick-jquery-tips/
> 
> Feel free to hit me over the head if I can improve the code.
> 
> Thanks,
> Rowan Lewis.


Thanks, nice! Here's what I found:


#2 I'd use a single chain for that:

$("ul").addClass("Replaced").find("> li").prepend("‒ ");

(Although I don't like presentational JavaScript very much and would use 
a solution with pure CSS (:before pseudo element) and graceful 
degradation in IE).


#4 Avoid the hardcoded string, so you can change it easily in *one* 
place. In order to not forget keyboard users I'm using the focus event 
here instead of click:

$("#SearchText").focus(function() {
     if (this.value == this.defaultValue) {
        this.value = "";
     }
});

Here's an enhanced solution which switches back to the default text if 
the field is left empty:

$("#SearchText")
     .focus(function() {
         if (this.value == this.defaultValue) {
            this.value = "";
         }
     })
     .blur(function() {
         if (this.value == "") {
             this.value = this.defaultValue;
         }
     });

This could become a nice micro plugin:

jQuery.fn.searchField = function() {
     return this.focus(function() {
         if (this.value == this.defaultValue) {
            this.value = "";
         }
     }).blur(function() {
         if (this.value == "") {
             this.value = this.defaultValue;
         }
     });
};

(Untested)

So with that you can write:

$("#SearchText").searchField();

And of course it's chainable:

$("#SearchText").searchField().attr('autocomplete', 'off');


Cheers, Klaus




_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to