Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting hint text in a text field in Ruby on Rails

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

like image 615
rajaramyadhav Avatar asked Feb 21 '11 22:02

rajaramyadhav


2 Answers

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.

like image 78
wuputah Avatar answered Nov 10 '22 09:11

wuputah


Try this jquery plugin

http://plugins.jquery.com/project/hint

or choose here:

http://plugins.jquery.com/plugin-tags/hint

For prototype

http://davidchambersdesign.com/autopopulating-input-fields-with-prototype/

like image 27
fl00r Avatar answered Nov 10 '22 08:11

fl00r