I am quite new to jquery and javascript to be honest and I am having trouble with checkboxes.
This works as #checkAll is a checkbox used for checking all the other checkboxes with class "check":
function test() {
if($("#checkAll").prop('checked')) {
$(".check").prop('checked', true);
} else {
$(".check").prop('checked', false);
}
}
However, I want it to work when I select any checkbox. So I tried putting the function to all checkboxes as a click event, just using onclick="test()".
I think the problem is that I am trying to use $(this) but I must be using it wrongly, what I have is:
function test() {
if($(this).prop("checked")) {
$(".check").prop("checked", true);
} else {
$(".check").prop("checked", false);
}
}
"$(this)" also has the class "check", so maybe it's to do with that.
Ultimately I want to check one box and it check all previous checkboxes, not ones that come afterwards. I think if I get my head around why this isn't working then I should be able to figure that out. I'm not lazy, just learning!!
I have searched for an answer but cannot find one. I'm hoping someone can help.
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire