0

the elements:

<div><input type="checkbox"></input></div> // <-- checked
<div><input type="checkbox"></input></div> // <-- checked
<div><input type="checkbox"></input></div> // <-- unckecked
<div><input type="checkbox"></input></div> // <-- checked

I want to use nextUntil() with the div-elemets to uncheck the boxes(child-elements) until box3 (first unchecked). Is there a way?

3 Answers3

0

You can try to use :lt() selector instead:

$("div:lt(3)").find('input').prop('checked', false);

This will add checked property for input element inside div before 4th position.

I used 3 here because lt() is 0-based index.

Fiddle Demo

Felix
  • 37,892
  • 8
  • 43
  • 55
0
$('input[type=checkbox]').each(function () {
    if ( ! this.checked ) return false;
    this.checked = false;
});

Note: returning false from within the each callback breaks out of the loop.


Here's the fiddle: http://jsfiddle.net/VCD94/

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
0

The checkboxes being in different containers makes this rather complicated. The checkboxes in your example are NOT siblings to each other, which is why nextUntil (or rather prevUntil ) wont work here.

$("input:checkbox:not(:checked)").parent().prevAll("div").find("input:checkbox").prop("checked", false);

That is assuming its not always the same checkbox that is checked.

Thaylon
  • 453
  • 5
  • 12