I have a todo/task list type of app built with Rails.
I have a list of categories with their respective tasks below the name of the category.
I have a checkbox next to each task that when I click it, I want the form for that particular task to submit and update the task to be either complete/imcomplete. I have a jQuery function to do this:
$(function(){
$('input:checkbox').on('click', function(){
$(this).parents('form').submit();
});
});
My form looks like this (in HAML):
- form_class = task.complete? ? "edit_task complete" : "edit_task"
= form_for task, remote: true, html: {:class => form_class } do |f|
= f.check_box :complete
= f.label :complete, task.name
= link_to task_path(task.id), remote: true, method: :delete, class: "delete-task" do
%i.icon-remove.pull-right
The output HTML is this:
<form accept-charset="UTF-8" action="/tasks/338" class="edit_task" data-remote="true" id="edit_task_338" 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="q7bvSGPr1IDf1p2/SKsssbdiQj+NBWmg/C6zPB3x+jM=">
</div>
<input name="task[complete]" type="hidden" value="0">
<input checked="checked" id="task_complete" name="task[complete]" type="checkbox" value="1">
<label class="338" for="task_complete" id="task-label">another task</label>
<a href="/tasks/338" class="delete-task" data-method="delete" data-remote="true" rel="nofollow">
<i class="icon-remove pull-right"></i></a>
</form>
The problem is that when I click ANY checkbox, instead of finding that particular task's form, it only ever selects and toggles the very first task on the page.
Any help would be much appreciated.
Thanks
.closestinstead of.parents.