Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select tag : How to use params to set the selected value?

I have a select tag with 2 options :

 select_tag :type,  options_for_select("<option value = 'produit1'>Produit1</option><option value = 'Produit2'>Produit2</option>", params[:product]), id: "different_option_value_html", onchange: "this.form.submit();", include_blank: "Which products ?"

When one of this options are selected, the form is automatically submit. I would like to keep the params in first position. I mean visible without clicking on the select, at the place of "which products".

like image 238
Orsay Avatar asked Nov 02 '16 08:11

Orsay


People also ask

How to set value in select tag?

The select tag in HTML is used to create a dropdown list of options that can be selected. The option tag contains the value that would be used when selected. The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute.

How to Get default value of dropdown in JavaScript?

var selectbox_defaultOption = document. querySelector("#some_dropdown option[selected]");


1 Answers

I finally change the syntax and it works fine using :options_for_select(:collection, :selected)

So there is my code now :

= select_tag :product,  options_for_select(["Product1", "Product2"], params[:product]), id: "different_option_value_html", onchange: "this.form.submit();", include_blank: "Which product ?"
like image 187
Orsay Avatar answered Oct 22 '22 05:10

Orsay