Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autocomplete Ruby on Rails form_for using HTML5 Datalist

I have a form with text field:

<%= form_for @person do |f| %>
  <%= f.label :first_name, "First Name" %>:
  <%= f.text_field :first_name %>
  <%= f.submit %>
<% end %>

How do I use the HTML5 Datalist element to autocomplete the text field?

like image 905
Bruno Avatar asked Feb 04 '26 05:02

Bruno


1 Answers

This works for me on Rails 4.2beta.

<%= form_for @person do |f| %>
  <%= f.label :first_name, "First Name" %>:
  <%= f.text_field :first_name, list: "names" %>
  <datalist id="names">
    <option value="Kalle">
    <option value="Jarmo">
    <option value="Pekka">
  </datalist>
  <%= f.submit %>
<% end %>
like image 126
luopio Avatar answered Feb 05 '26 21:02

luopio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!