views:

28

answers:

1

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. http://stackoverflow.com/questions/2360465/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.

+1  A: 

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);
  }
});
slainer68
It is great or so it looks but unfortunately I am using ajax.
necker
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).
slainer68
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