The code for this is probably simple, but I’m not hot on Jquery yet and I couldn’t find a solution online.
In my view, users need to be able to select from a variety of plant images using a rails check_box_tag form submit. When a user clicks on a plant image, I want its accompanying check box to become selected.
My view:
<%= form_for @project.prelationships.build(:pfollowed_id => @project_id) do |f| %>
<% Plant.all.each do |plant| %>
<h>
<%= image_tag plant.image_path %>
<%= check_box_tag "prelationship[pfollower_id][]", plant.id %>
</h>
<% end %>
<%= hidden_field_tag :project_id, @project.id %>
<%= f.submit "Pfollow" %>
<% end %>
And in my application.js:
$(function () {
$("h").click(function () {
$(this).hide().parent().find(':submit').click();
});
});
The problem with that javascript code right now is that it submits the form when you click on an image instead of simply checking off that image’s checkbox. How do I modify that one line of code to return a checked checkbox on onclick? I’ve seen a lot of sample solutions online, but they have never dealt with rails’ check_box_tag, which seems to require a unique answer.
If I guess what you mean this should solve it:
See it working at:
http://jsfiddle.net/RZUfs/