yerassyl
yerassyl

Reputation: 3048

Passing parameter to onchange of select in Rails

<%= f.select :id, options_from_collection_for_select(
              @rtypes, "id", "typeName"), 
              {include_blank: true },																				                            
:onchange => ShowSubTypes() %>	

Here I have options for select, what I want is to pass an id as a parameter to ShowSubTypes() function. My @rtypes variable returns an array, therefore I cant just pass @rtypes.id.

Upvotes: 0

Views: 2610

Answers (1)

Thong Kuah
Thong Kuah

Reputation: 3283

<%= f.select :id, options_from_collection_for_select(
              @rtypes, "id", "typeName"), 
              {include_blank: true },                                                                                                           
:onchange => "ShowSubTypes($(this).val())" %>   

Basically you can accomplish this via Javascript. this within the context of the onChange is the <select> input. I have assumed jQuery to obtain the value of the selected option which corresponds to "id" of @rtypes.

In plain Javascript, I think it's this.value

Upvotes: 5

Related Questions