simon
simon

Reputation: 21

how can I reference f.select tag id in rails to get value selected

Am having trouble getting the id the value from a f.select tag in rails . I need to pass the value in javascript so i can perform a calculation but the html rendered does not have the id and the only value showing is the default no matter what I choose.

in view

<%= form_for([@event, @event.reservations.new]) do |f| %>

<div class="col m6 s12">
  <label for="Guests">Guests</label>
  <%= f.select :guests, [["1",1], ["2",2], ["3", 3 ], ["4", 4 ], ["5", 5 ], ["6", 6 ], ["7", 7 ], ["8", 8 ], ["9", 9 ], ["10", 10 ], ["11", 11], ["12", 12 ], ["13", 13], ["14", 14], ["15+", 15 ]], id: "guests"   %>                   
</div>

in javascript

var guestSize = document.getElementById("guests").value;

This is what is being rendered in the html

<input type="text" class="select-dropdown" readonly="true" data-activates="select-options-9586aaf6-e36f-d805-3a71-7ba42d7bbec3" value="1">

No 'name' and 'id' showing up .

Upvotes: 1

Views: 2578

Answers (1)

Ronan Lopes
Ronan Lopes

Reputation: 3398

try

<%= f.select :guests, [["1",1], ["2",2], ["3", 3 ], ["4", 4 ], ["5", 5 ], ["6", 6 ], ["7", 7 ], ["8", 8 ], ["9", 9 ], ["10", 10 ], ["11", 11], ["12", 12 ], ["13", 13], ["14", 14], ["15+", 15 ]], {}, id: "guests"   %>                   

The empty hash as third argument may be relevant

Upvotes: 2

Related Questions