1

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 .

2
  • can you edit with the html element on the rendered page? Jun 21, 2016 at 13:42
  • am sorry i don't quite understand what you mean
    – simon
    Jun 21, 2016 at 14:17

1 Answer 1

2

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

3
  • Hey Thanks, it worked! you're a lifesaver. Why is an empty hash is required?
    – simon
    Jun 21, 2016 at 14:38
  • Will mark this as the answer as soon I have enough points to upvote
    – simon
    Jun 21, 2016 at 14:39
  • The method select has the format: select(method, choices = nil, options = {}, html_options = {}, &block). So you could pass as include_blank:true or another option you want, but if don't, just leave it blank as above. Thanks! Jun 21, 2016 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.