Setting hint text in a text field in Ruby on Rails

3k Views Asked by At

Can some one suggest the best way for setting hint text(not default text) for a text field in Ruby on Rails. Currently I am using this:

<%= text_field_with_auto_complete
  "customer", 
  :contact_person, { 
    :value => 'last, first',
    :style => 'color:#aaa;width:11em;', 
    :onfocus => "if(this.getValue()=='last, first'){this.clear();this.style.color = '#000';}",
    :onblur => "if(this.getValue()==''){this.setValue('last, first');this.style.color = #aaa';}" 
  } %>

The text field is attached to a model and in turn to a value in a database. Also, the same html that is used in index.html is also used in edit.html and hence when I try to edit the field, the default value shows up instead of the value from the database. What is the correct way to do this?

Note: I am not trying to set a default value but just a hint to what needs to be entered in the text box.

Thanks,

Raja Ram

2

There are 2 best solutions below

1
On
1
On

I'd recommend using the HTML 5 placeholder attribute, and then using a jQuery plugin to make it work on older browsers (like this one, but there are many others on Google).

You can see this technique used in production here.