JQuery checkbox error
I'm working on a jQuery based todo list interface and hit the wall a bit. The jQuery I'm working with is a bit of a hack from the various tutorials I've read since I'm a bit of a newbie.
$('#todo input:checkbox').click(function(){
var id = this.attr("value");
if(!$(this).is(":checked")) {
alert("Starting.");
$.ajax({
type: "GET",
url: "/todos/check/"+id,
success: function(){
alert("It worked.")
}
});
}
})
This is the HTML I am using,
<div id="todo">
<input type="checkbox" checked="yes" value="1"> Hello, world. <br />
</div>
Any help on this would be greatly appreciated. For reference, thereason I have warnings in jQuery for debugging. The reason I can tell the code is not working is because I am not getting these warnings. Thanks.
a source to share
Just a quick glance and I think the part if(!$(this).is(":checked"))
might be the problem. Try something likeif( !$(this).attr('checked') )
Also: Your ajax statement url doesn't look right. It seems to be an absolute path, not a relative one for me.
One more thing: not worth it to var id = this.attr("value");
be var id = $(this).attr("value");
?
Let me know if these tips help. Otherwise, general advice:
In your code, you seem to be testing three things at the same time. It must first bind the event to the correct checkbox. Then it has to pass the condition and then it has to make a successful ajax call. We only get feedback if all three things work, otherwise, if someone is misplaced, then we cannot know where the failure occurred. Check every detail to make sure it works and then you know where to focus your efforts. :-)
a source to share