Vijikumar M
Vijikumar M

Reputation: 3764

How to add default value in multiple select box?

I am using multiple select box to get collection of values. Now i want to set a default value for that. I tried the below but its not working. If you have any idea please share.

 <select name="protocols[]" multiple="multiple" id="form-field-select-2" class="form-control" default="hls">
 <%videos.each do |option|%>
 <option><%=option%> </option>
 <%end%>
 </select>

Upvotes: 0

Views: 6261

Answers (2)

Surya
Surya

Reputation: 16022

This code should do the trick:

<select name="protocols[]" multiple="multiple" id="form-field-select-2" class="form-control">
  <%videos.each do |option|%>
    <option value=<%= option %> <%= option == 'hls' ? 'selected="selected"' : '' %>><%=option%> </option>
  <%end%>
</select>

Upvotes: 2

harsh4u
harsh4u

Reputation: 2610

I think this question is already exits. You need to find first solutions then ask if not found.

I hope you can find your answer in below link: click

Where @arr_selected is saved selected values array

<% @arr_selected = ['first','second'] %>    
     <select name="protocols[]" multiple="multiple" id="form-field-select-2" class="form-control" default="hls">
     <%videos.each do |option|%>
     <option <% (@arr_selected.include?(option)) ? "selected" : "" %> ><%=option%> </option>
     <%end%>
     </select>

Upvotes: 1

Related Questions