I have a rails view (erb) that displays some checkboxes (populated data from seed.rb).. When certain checkboxes are selected i need a div to appear below it that collects more information.. For example: the user checks "anniversary" and a div below it appears asking for the date. Is jquery the best way to do this? Coffescript?
*Note: Im using the Wicked gem to create a multistep from
Heres the view:
<%= render layout: 'form' do |f| %>
<% for holiday in Holiday.find(:all) %>
<label class="checkbox">
<%= check_box_tag "user[holiday_ids][]", holiday.id, @user.holidays.include?(holiday) %>
<%= holiday.name %>
</label>
Heres the rendered html (the form part anyway):
<form accept-charset="UTF-8" action="/user_steps/interests" class="edit_user" id="edit_user_3" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="lub8Fb5pvkxwgb3hGT66U14QBNKLOLfEwaLLw9Ts2WU=" /></div>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="1" />
New Years Day
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="2" />
Valentine Day
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="3" />
Easter
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="4" />
Mother Day
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="5" />
Father's Day
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="6" />
Halloween
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="7" />
Thanksgiving
</label>
<label class="checkbox">
<input id="user_holiday_ids_" name="user[holiday_ids][]" type="checkbox" value="8" />
Christmas
Thanks!
Hiya demo http://jsfiddle.net/z862m/ and from you site link provided solution is here: http://jsfiddle.net/2UfBy/
So the demo will do what you mentioned. i.e. WHen the checkbox is ticked the corresponding div will show and when unchecked it won't. B-)
[quote] When certain checkboxes are selected i need a div to appear below it that collects more information.. For example: the user checks "anniversary" and a div below it appears asking for the date...[unquote] :)
Jquery code
Html
JQuery is a good way to do it. You need the appropriate require statements in your application.js file to use JQuery
An easy way to pull it off would be to surround everything you want to show and/or hide with a span tag, and then use JQuery to trigger the show/hide on your span tag from your *.js.erb file
You can use the following respond_with block to specify a format.js (javascript) response. The code goes in your controller's method name matching the form's name:
This will cause hide_show_form.js.erb to run first, before the HTML in your hide_show_form.html.erb ever renders.
hide_show_form.js.erb
hide_show_form.html.erb
This example specifies an :onclick property of a check_box_tag to call a JavaScript function, showHiddenForm() in the js.erb file.
I think you should do something like
This is not the perfect code but you coud do it with .hide() and .show() method.
I hope this will help you.
Thanks.