views:

386

answers:

2

I have a partial that renders a select box using the following method:

<%= collection_select 'type', 'id', @types, "id", "name", 
  {:prompt => true}, 
  {:onchange => 
             remote_function(
              :loading => "Form.Element.disable('go_button')",
              :url => '/sfc/criteria/services', 
              :with => "'type_id=' + encodeURIComponent(value) + '&use_wizard=#{use_wizard}'"),
 :class => "hosp_select_buttons"
} %>

This partial gets used 2 times on every page, but at one point I need to get the value of the first select box. Using:

$('type_id')

returns the second select box. Is there a way to find the first one easily? Should I fix this using javascript or by redoing my partial?

Note: the dropdowns do get rendered in separate forms.

+1  A: 

I figured out one way to do this by assigning an ID in the html_options block. I already am passing in a value for use_wizard, so I can append that value on to the ID to differentiate between the two dropdowns.

:id => "type_id_wizard_#{use_wizard}"
Kyle Boon
+1  A: 

Yes, each element does need a unique ID, the page probably also fails HTML validation. Also, unless these are in 2 different forms you'll have a conflict with the CGI parameter name.

If they are in 2 different forms you can probably get away with just setting the :id as you posted, if they are the same form you need to abstract the parameter name too:

collection_select 'type', "id_wizard_#{use_wizard}"...