nextUntil with .each - can't figure it out...

nextUntil with .each - can't figure it out...

Having some issues using nextUntil that I just can't figure out. So I have a table with a checkbox as the first cell. When the checkbox is clicked, it adds a class 'activated' to the parent tr, and also goes up to a designated header row (class='category') and adds 'activatedHead' class to that row. That all works.

When a function is called, jQuery is supposed to grab each row with 'activatedHead' class, iterate through, grab each row that has class 'activated' under that 'activatedHead' row, UNTIL it reaches another header row - then grab the contents of the cells within each 'activated' row. (I know this is confusing - code is coming...)

The issue is that using nextUntil with each, jQuery doesn't stop at the next header row - it moves on and grabs cells from all rows with class 'activated'.

So my table after clicking on some checkboxes:

  1. <tr class="category activatedHead">
  2.       <td colspan="8">I-495 Capital Beltway</td>
  3. </tr>
  4. <tr class="activated">
  5.       <td class="check"><input type="checkbox" class="checkMe" /></td>
  6.       <td class="time">3:42 PM</td>
  7.       <td class="crt">Major</td>
  8.       <td class="type">ACC</td>
  9.       <td class="dir">Westbound</td>
  10.       <td class="loc">Near Connecticut Ave</td>
  11.       <td class="desc">3 right lanes closed for a major accident. Traffic backed up for 2 miles. Police expect cleanup to take another hour. Use flying carpet as alternate route.</td>
  12. </tr>
  13. <tr>
  14.       <td class="check"><input type="checkbox" class="checkMe" /></td>
  15.       <td class="time">3:46 PM</td>
  16.       <td class="crt">Moderate</td>
  17.       <td class="type">Overturned Truck</td>
  18.       <td class="dir">Outer Loop</td>
  19.       <td class="loc">Near Connecticut Ave</td>
  20.       <td class="desc">I-495 Outer Loop Near Connecticutt Ave - Right lane closed due to Overturned truck. Traffic moving by slowly on the left.</td>
  21. </tr>
  22. <tr class="category activatedHead">
  23.       <td colspan="8">I-95 (Virginia)</td>
  24. </tr>
  25. <tr class="activated">
  26.       <td class="check"><input type="checkbox" class="checkMe" /></td>
  27.       <td class="time">3:53 PM</td>
  28.       <td class="crt">Minor</td>
  29.       <td class="type">VOL</td>
  30.       <td class="dir">Northbound</td>
  31.       <td class="loc">Newington</td>
  32.       <td class="desc">Minor delays as you pass the 7100 interchange due to an old man in a Cadillac who has his left blinker on in the left lane.</td>
  33. </tr>
The jQuery is supposed to only return the cells in class 'activated' until it reaches the next tr class='category':

  1. jQuery('tr.activatedHead').each( function(i) {
  2.       var header = jQuery(this).text();
  3.       oBody.append('<table id="category' + i + '" ><thead><tr><th colspan="6">' + header + '</th></tr></thead><tbody></tbody></table>');
  4.       console.log("header: " + header);
  5.       jQuery(this).nextUntil('tr.category', 'tr.activated').each( function(j) {
  6.             var oTable = 'table#category' + i + ' tbody';
  7.             oBody.find(oTable).append('<tr id="incident' + j + '"></tr>');
  8.             jQuery(this).children('td').each( function(k) {
  9.                   var class = jQuery(this).attr('class');
  10.                   if ( (class == "check") || (class == "edit") ) { 

  11.                   } else {
  12.                         var data = jQuery(this).text();
  13.                         var oTr = 'tr#incident' + j + '';
  14.                         oBody.find(oTr).append('<td class="' + class + '">' + data + '</td>');
  15.                         console.log("array: " + data);
  16.                   }
  17.             });
  18.       });
  19. });
But instead it is returning the cells of the first class='activated', plus the cells of the second class='activated' - and then going back and returning the second class='activated' again.... Line 5 of the jQuery above is where I'm missing something. I know this is an involved post - hopefully someone can tell me what I'm missing here. Thanks for any help, and apologies if I've just confused the heck out of everyone.

Andrew