I have a few checkbox elements and I want to disable the next (or previous).
My code looks like this:
<td>
<input type="checkbox" class="chkOk" />
</td>
<td>
<input type="checkbox" class="chkNotOk" />
</td>
<!-- Futher checkboxes -->
I tried:
$(function () {
$('input[type=checkbox]').bind("click", function () {
$(this).next('input[type=checkbox]').attr("disabled");
});
});
And also:
$(this).siblings('input[type=checkbox]').attr("disabled");
But it does not work. Please help.
You’ll have to go up to the parent
tdand find the next checkbox from there:This gets the
tdusingparent, then gets all the following siblings (nextAll) of thattd, reduces the matched set to only thosetdelements which contain a checkbox (has) and then gets the first one (first) in the remaining set.Note that
disabledis a property, and you should be usingpropto set it, notattr:The problem you were having is because you are looking for siblings of the clicked checkbox, and in your example HTML they don’t have any siblings.