Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 2.50 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to check if the parent element of an element is in itself the last child with javascript or jquery
  2. $('body').delegate('a[data-view="showhide"]', 'click', function (e) {
  3.      var thisElem = $(this),
  4.          thisTbody = thisElem.closest('tbody');
  5.  
  6.      e.preventDefault();
  7.  
  8.      //check to see if this element's parent is a last child
  9.      if (thisTbody.filter('[data-state]').is(':last')) {
  10.         console.log('this is the last tbody of its kind called 'data-state'');
  11.         //...Do something
  12.      }
  13. });
  14.        
  15. <table>
  16.   <tbody data-state="closed">
  17.     <tr><td><a href="#" data-view="showhide">cell 1</a></td></tr>
  18.   </tbody>
  19.   <tbody data-state="closed">
  20.     <tr><td><a href="#" data-view="showhide">cell 2</a></td></tr>
  21.   </tbody>
  22.   <tbody data-state="closed">
  23.     <tr><td><a href="#" data-view="showhide">cell 3</a></td></tr>
  24.   </tbody>
  25.   <tbody>
  26.     <tr><td>cell not important</td></tr>
  27.   </tbody>
  28. </table>
  29.        
  30. if (!thisTbody.nextAll('tbody[data-state]')[0]) {
  31.     // It's the last `tbody` with a `data-state` attribute in its
  32.     // enclosing table
  33. }
  34.        
  35. $(document).delegate('a[data-view="showhide"]', 'click', function (e) {
  36.      var thisElem = $(this),
  37.          thisTbody = thisElem.closest('tbody[data-state]'),
  38.          thisIndex = thisTbody.index(),//get the index of the currently selected `tbody` element
  39.          thisCount = thisElem.closest('table').find('tbody[data-state]').length;//get the number of `tbody` elements with the `data-state` attribute
  40.  
  41.      e.preventDefault();
  42.  
  43.      //see if the current index is equal to the total number of `tbody[data-state]` elements (remember that `.length` starts at one)
  44.      if (thisIndex == (thisCount - 1)) { /*It has been found that this is the last element*/ }
  45. });
  46.        
  47. <table>
  48.   <tbody class="closed">
  49.     <tr><td><a href="#" class="showhide">cell 1</a></td></tr>
  50.   </tbody>
  51.   <tbody class="closed">
  52.     <tr><td><a href="#" class="showhide">cell 2</a></td></tr>
  53.   </tbody>
  54.   <tbody class="closed">
  55.     <tr><td><a href="#" class="showhide">cell 3</a></td></tr>
  56.   </tbody>
  57.   <tbody>
  58.     <tr><td>cell not important</td></tr>
  59.   </tbody>
  60. </table>
  61.        
  62. $(document).delegate('a.showhide', 'click', function (e) {
  63.      var thisElem = $(this),
  64.          thisTbody = thisElem.closest('tbody.closed'),
  65.          thisIndex = thisTbody.index(),
  66.          thisCount = thisElem.closest('table').find('tbody.closed');
  67.  
  68.      e.preventDefault();
  69.  
  70.      //check to see if this element's parent is a last child
  71.      if (thisIndex == (thisCount - 1)) { /*It has been found that this is the last element*/ }
  72. });