views:

734

answers:

2

I have a select Menu with a few options

  <p>
    <%= f.label :reason %><br />
    <%= f.select :reason, Confirmation.reasons.collect {|p| [ p[:name], p[:id] ] }, { :include_blank => true } %>
  </p>

The last being id "5" and name = "Other"

If and only if they choose other I want a hidden text_area with a div id = "other" to be shown to allow the user to type...

How can I do this simply?

  <%= link_to_function "Show me other" do |page| 
    page[:other].toggle
  end %>

I can toggle it but I want to show it on the selection of the "Other" in the select box? Is this a job for observe_field?

+2  A: 
<%= f.select :reason, Confirmation.reasons.collect {|p| [ p[:name], p[:id] ] }, { :include_blank => true }, { :onchange => "if (this.value == '5') { $('otherform').show(); }" } %>

Got it!

holden
+2  A: 

Your own answer is fine, but for future reference, observe_field doesn't have to make an Ajax call. You can use :function to specify JavaScript code to run locally instead.

e.g.

<%=observe_field 'reason', :frequency => 1, 
    :function => "if ($('reason').value == '5') { $('otherform').show(); } else { $('otherform').hide(); }" %>
mikej
oh, i like yours. thanks!
holden