0

I have this form:

<%= form_for(@post) do |f| %>
<%=f.collection_select :post_id, Board.where(:user_id => current_user.id), :id, :name %>
<%= f.submit "Post it"%>
<% end %>

I want to know how add new option without reload page with input button through jquery:

I want add the option to collection_select before submit form

I'm using jquery ujs https://github.com/rails/jquery-ujs

Example:

enter image description here

hyperrjas
  • 10,666
  • 25
  • 99
  • 198

1 Answers1

0

if the option you want to add is already on the page this post has the answer.

if you wanted to do it with ajax you would need to get the options though a call to ajax to get the array then iterate over it

update:

   var input_value =  $('#myInput').val()
   $('#mySelect').append(
        $('<option></option>').val(input_value.replace(' ', '_')).html(input_value)
    );

change append to prepend if you want it at the top which might be a bit more user friendly

the replace element might need work depending on what the users are likely to input and how you want to store the new options.

and was not sure how you wanted to fire this, so you need to bind it to the create i guess...

Community
  • 1
  • 1
nodrog
  • 3,532
  • 2
  • 25
  • 31
  • thank you nodrog. The option to add, is a new option. This new option is added by use through submit input. I have uploaded a example photo in question! – hyperrjas Feb 13 '12 at 17:03