Reputation: 619
I don't know if this is even possible, but in my rails app, I have this currently to put text in a text_field to be a description instead of having an external label:
<% inside = "Search for Customers" %>
<%= text_field_tag :search, params[:search],
:style => "color:#aaa;",
:value => inside, #defined above
:onfocus => "if(this.getValue()=='#{inside}'){this.clear();this.style.color = '#000';}",
:onblur => "if(this.getValue()==''){this.setValue('#{inside}');this.style.color = '#aaa';}" %>
<%= submit_tag "Search", :name => nil %>
This works great, but if I don't enter anything in, it searches for "Search for Customers", which is not what I intended. Is there a way to fix this?
Thanks
Edit:
I've added a :onSubmit => "if(this.getValue()=='#{inside}'){this.clear();}"
to the text_field_tag, but it's still not doing anything. I've also tried :onbeforesubmit
and even a simple alert('test')
but it's not working for some reason.
Upvotes: 0
Views: 309
Reputation: 1194
There is a new placeholder attribute for inputs in HTML5, it does exactly what your asking for, but wont work for everyone yet.
http://diveintohtml5.ep.io/detect.html#input-placeholder
You could use it and fallback to populating and clearing the field with the onsubmit event for people that have older browsers.
Upvotes: 3
Reputation: 5426
You may also simply ignore the request at the controller level if the search field is blank or if the search parameter corresponds to your inside
string, something like:
def search
if params[:search].blank? || params[:search] == "Search for Customers"
return
else
do_whetever_you_want
end
end
Upvotes: 1
Reputation: 27620
Not sure if rails makes it different, but you can usually use:
<form onbeforesubmit="someFunction()">
And then in somefunction() clear the textfield.
Upvotes: 0
Reputation: 46914
Fill inside by params[:search] if define
<% inside = params[:search] || "Search for Customers" %>
Upvotes: 0