Code Snippet
Clear Default Search String on Focus
$("#s")
.val("Search...")
.css("color", "#ccc")
.focus(function(){
$(this).css("color", "black");
if ($(this).val() == "Search...") {
$(this).val("");
}
})
.blur(function(){
$(this).css("color", "#ccc");
if ($(this).val() == "") {
$(this).val("Search...");
}
});
- Set value of field to "Search..."
- When field comes into focus, set color to black.
- If value is default, remove it.
- When field goes out of focus, set color back to light gray.
- If value is empty, put back default value
I would like the text to only go back to grey if it is the default ‘search…’ text..
blur(function(){if ($(this).val() == "") {
$(this).css("color", "#ccc");
$(this).val("Search...");
}
i think this one is better, it will work for all textboxes and it uses the in page value instead of declaring the value in jquery.
swap_val = [];$(".swap").each(function(i){
swap_val[i] = $(this).val();
$(this).focusin(function(){
if ($(this).val() == swap_val[i]) {
$(this).val("");
}
}).focusout(function(){
if ($.trim($(this).val()) == "") {
$(this).val(swap_val[i]);
}
});
});
This is perfect and has made my day! Thanks.
why it doesn’t work for me?
this is my page
please take a look, I’m going crazy
http://www.athenarestauri.com/prova-jquery-2.html
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.