I’m having difficulty identifying the input id for some rails form dropdown elements for a few jquery validations I want to perform client side.
I was easily able to pickout the text field ids, but these dropdown fields are tricky.
Any suggestions? Can I pick out the id from the form element before it’s rendered on the front end?
If your form comes in as f, here’s a trick to get a <select>'s id:
<%= f.select :my_dropdown, options %>
The ID is <%= f.field_id(:my_dropdown) %>
var mySelect = $(document.getElementById(<%= f.field_id(:my_dropdown) %>));
The name is <%= f.field_name(:my_dropdown) %>
Now in jQuery you can just reference mySelect and it’s a jQuery-enabled client-side object.