0

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.

Community
  • 1
  • 1
necker
  • 6,733
  • 9
  • 31
  • 40

1 Answers1

1

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);
  }
});
Nicolas Blanco
  • 11,164
  • 7
  • 38
  • 49
  • It is great or so it looks but unfortunately I am using ajax. – necker Oct 30 '10 at 09:54
  • i tell you how to do this using Unobscursive Javascript, this is clearly the best way. This does not matter if you're using Ajax calls or not, you can check checkboxes if the partial is rendered from an ajax call or not (you should buy a book about jQuery). – Nicolas Blanco Oct 30 '10 at 10:07
  • It was a stupid comment from me. I am using prototype and some scriptaculous javascript libraries and I am not sure if I should add another library just because of one case that it would come handy in. Thank you for taking time to answer though! – necker Oct 30 '10 at 11:35