Hi - I’m trying to implement a search box that has a pre-filled
message “enter search text.” The first time it’s selected, I’d like
it to clear out, but after that, I’d like it to remain. I imagine
lots of people have done this, any ideas how to accomplish this?
Not sure exactly what you mean by “The first time it’s selected, I’d
like
it to clear out, but after that, I’d like it to remain”, but I do much
the same functionality with this line:
sorry, I left out a couple of details. in body onload, I have to loop
thru the search form and put the hint value in the text box. Add this
to Event.addBehavior above
that’s pretty much what I did. I have several search forms that I
want to have the functionality you’re looking for. I give the search
forms a class “searchform”. The search text is always named “q” and I
have a hidden field called hint to customize the hint text. I am
using lowpro (google it) to separate the code from my view.
In view:
is js:
function notempty(fld) {
return fld.value!=null && fld.value.match(/\S/)!=null;
}
Event.addBehavior({
‘.searchform:submit’: function() {
// is query text not empty and not the hint text?
if (notempty(this.q) && this.q.value!=this.hint.value) {
elems = this.elements;
// remove the hint and empty fields so they don’t show in the
query string (optional)
for( i=elems.length-1;i>=0;i–)
if (elems[i].value=="" || elems[i].name==‘hint’)
elems[i].remove();
return true;
}
// you may want to display an alert instead of just returning false
return false;
},
‘input.search:blur’: function() {
if (notempty(this)) return;
this.style.color=’#444’;
this.value=this.form.hint.value;
},