Mark
Mark

Reputation: 33571

jquery nextUntil has element

I have a bunch of elements like this:

<div></div>
<span></span>
<table></table>
<div></div>
<span></span>
<div></div>

I need to check whether or not there's a table element in between the divs, and if so do something.

$('div').each(function () {
  if ($(this).nextUntil('div').include('table')) {
    $(this).addClass('got-a-table');
  }
}

Something like this? I know that there's no include method, is there something that can get me what I need?

Thanks.

Result should be like this:

<div class='got-a-table'></div>
<span></span>
<table></table>
<div></div>
<span></span>
<div></div>

Edit: a jsbin for quick testign: http://jsbin.com/aqoha/2/edit

Upvotes: 2

Views: 1893

Answers (2)

erisco
erisco

Reputation: 14329

$('div').each(function () {
  if ($(this).nextUntil('div').filter('table').length > 0) {
    $(this).addClass('got-a-table');
  }
});

Instead of include(), you want filter().

Upvotes: 2

Reigel Gallarde
Reigel Gallarde

Reputation: 65264

try

$('div').each(function () {
   var table = $(this).next('table');
   if (table) {
       if (table.next('div')){
            // do something.
       }       
   }
});

Upvotes: 0

Related Questions