$("#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]);
}
});
});
Yes nice. But something is quite disturbing with this solution. Imagine an input field that returns an error (ie. an email field you must fill). When it prompts back to correct your input, all field will be cleared on focus… Am I right?
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
You need to put your jquery functions inside:
$(document).ready(function(){
// do jQuery stuff
})
@Dyllon
$(“#searchfield”)
.val(“Search…”)
.css(“color”, “#999”)
.focus(function(){
$(this).css(“color”, “black”);
if ($(this).val() == “Search…”) {
$(this).val(“”);
}
})
.blur(function(){
$(this).css(“color”, “#999”);
if ($(this).val() == “”) {
$(this).val(“Search…”);
}else if($(this).val() != “Search…”){
$(this).css(“color”, “black”);
}
});
Hi, I’m new to jQuery!
Sorry for my confusion as I know this can be done quite simple in javascript, just like this:
Is it much simpler?