3

I have the following line in my form:

= text_field_tag :mother_name, nil, id: 'mother_name_autocomplete', class: 'form-group form-control', autocomplete: 'off', data: {provide: 'typeahead'}

How can I add a placehodler to this field?

4 Answers 4

12

if you are using text_field_tag then you can do something like this.

<%= text_field_tag :mother_name, nil, placeholder: 'Some text' %>

notice that second argument is nil.

4

With rails >= 3.0, you can simply use the placeholder option.

f.text_field :mother_name, :placeholder => "mother_name_autocomplete"

You can find more reference here: http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

1

For Example, id = "input_name", value = nil, placeholder = "Enter your name"

So the tag is provided as,

 <%= text_field_tag 'input_name', nil, placeholder: "Enter your name" %>

The above code will be generated as mentioned below,

<input type="text" name="input_name" id="input_name" placeholder="Enter your name">

Read more about the tags here : https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html

-3
<%= text_field_tag "search", 'Enter search term...' %>

Add an onclick event to empty it with jQuery:

<%= text_field_tag "search", 'Enter search term...', :onclick => 'if($(this).val()=="Enter search term..."){$(this).val("");};' %>

Reference:- Text Field Tag Placeholder Returning As Value

1
  • There's absolutely no need to do this, now that placeholder is properly supported as a DOM attribute. Aug 18, 2017 at 12:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.