views:

109

answers:

2

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

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

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

erisco
If you've seen this answer appear and disappear repeatedly, it is because I kept thinking it was going to work before I changed my mind. After actually testing the thing I am sure this version works now.
erisco
A: 

try

$('div').each(function () {
   var table = $(this).next('table');
   if (table) {
       if (table.next('div')){
            // do something.
       }       
   }
});
Reigel
I don't believe that works: http://jsbin.com/aqoha/2/edit
Mark