<table> <tr class="codesgroupheader"><td>Header1</td></tr> <tr><td>1</td></tr> <tr class="codesgroupheader"><td>Header2</td></tr> <tr><td>2</td></tr> <tr class="codesgroupheader"><td>Header3</td></tr> <tr><td>3</td></tr> <tr class="codesgroupheader"><td>Header4</td></tr> </table>
And here is the jQuery code
$('.codesgroupheader').not(':last').each(function() { alert($(this).nextUntil('.codesgroupheader').find('tr').size()); });
But he always returns me zero.
Your code does the following:
$(this) // <tr> <-- = this .nextUntil('.codesgroupheader')// All elements between this and <tr class="..."> .find('tr') // Seeks for a <tr> inside <tr> - Not found
If you want to select the number of <tr> elements with this class, use:
<tr>
alert($('tr.codesgroupheader').not(':last').length);
If you want to get the number <tr> between the current and the next <tr class=...> , use:
<tr class=...>
// this = any <tr> $(this).nextUntil(".codesgroupheader", "tr").length;
See the jQuery documentation for an additional reference and use of the nextUntil function.
nextUntil
this refers to the current tr element.
this
tr
nextUntil('.codesgroupheader') will find all the elements between this and the next tr.codesgroupheader .
nextUntil('.codesgroupheader')
tr.codesgroupheader
Your .find trying to search inside this set (therefore inside tr ). you can completely skip find , or if for some reason you think there might be other elements, use filter (or the second nextUntil parameter) nextUntil , which filters the current set.
.find
find
filter
So use
$('.codesgroupheader').not(':last').each(function() { alert($(this).nextUntil('.codesgroupheader').size()); });
or to filter
$('.codesgroupheader').not(':last').each(function() { alert($(this).nextUntil('.codesgroupheader').filter('tr').size()); });
or (using the second parameter nextUntil )
$('.codesgroupheader').not(':last').each(function() { alert($(this).nextUntil('.codesgroupheader', 'tr').size()); });
Source: https://habr.com/ru/post/1387768/More articles:C # html to pdf converter using wkhtmltopdf or any other free tools - c #Qt 4.8.0 - MySQL driver not specified - c ++Highlighting source syntax using code folding function - javascriptWhat is the difference between "x IS NULL" and "NOT (x IS NOT NULL)"? - sqlImproving chat security now.js / socket.io - node.jsStyling css for dropdown for all browsers and Windows / OSX - htmlSmall built-in synthesized speech libraries / sentences - embeddedhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1387771/prevent-commiting-files-to-svn-with-warnings-using-eclipse&usg=ALkJrhgGIEfAOLZ4b-8_DtOnvb0b3cKI2QCustom dropdown using javascript and css - javascriptAndroid library project: R.id cannot be resolved or is not a field - androidAll Articles