Reputation: 7163
What i would like to do is when a user clicks an image of a horse that a checkbox is then set to true or false. I have an array of horses and each horse has a checkbox.
The biggest problem I have is setting the ID of my checkbox element. I tried sth like. Use custom id for check_box_tag in Rails
<% @horses.each do |s| %>
<div class="positionLeft roundedEdge5" style="padding:0 8px;" ondblclick="document.getElementById('line_horses_ids__<%=s.id%>').value = true">
<% if checked == true %>
<%= check_box_tag "line[horses_ids][]", s.id, :checked => true %>
<%= label_tag "line[horses_ids][][#{s.id}]", s.name %>
<% else %>
<%= check_box_tag "line[horses_ids][]", s.id %>
<%= label_tag "line[horses_ids][][#{s.id}]", s.name %>
<% end %>
</div>
<%end%>
But I still cant get the checkbox to change to true? What am i doing wrong? Any help would be appreciated.
Upvotes: 0
Views: 1303
Reputation: 11299
By using jQuery, you don't have to use custom ids or put javascript in your html... great isn't it?
If the check box is just next the horse image, just put a class "image" for the image and a class "check-box" for the checkbox and add this JS in your application.js :
$(document).ready(function() {
$(".image").click(function(this) {
$(this).next(".check-box").attr("checked", true);
}
});
Upvotes: 1