1

I was wondering how to use jQuery's :gt() in an inclusive way. I am trying to show/hide table rows dynamically.

$('#' + tbodyId + ' > tr:gt(' + newRowStart + '):lt(' + rowsToShow + ')').show();

If i try to show the first 5 rows say, newRowStart = 0 and rowsToShow = 5. This will not show the first row. Setting it to -1 doesn't work either. It would be very helpful if there was an inclusive method like :gt(). Does anyone know how to do this?

1
  • maybe $('#' + tbodyId + ' > td').filter(':gt(variable)').filter(':lt(variable)').show(); works? Commented May 3, 2013 at 12:25

2 Answers 2

4

I would just use .slice [docs]:

$('#' + tbodyId + ' > tr').slice(newRowStart, newRowStart + rowsToShow).show();
// or if rowsToShow is an index actually:
$('#' + tbodyId + ' > tr').slice(newRowStart, rowsToShow).show();

It's also a bit easier to read.

It would be very helpful if there was an inclusive method like :gt()

Not that I know of. If you want to include all elements from a given index on, you either have to use :gt(index-1) or omit :gt completely if the index is 0.

Sign up to request clarification or add additional context in comments.

Comments

2

One option is to use slice():

$('#'+tbodyId)
  .find('tr')
  .slice( newRowStart, newRowStart + rowsToShow ) // inclusive of starting point
  .show();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.