jquery - get all rows except the first and last

JqueryJquery Selectors

Jquery Problem Overview


i want to dynamically add a class to all rows of a table except the first and last row. how would i do this without assigning a css class to the rows to identify them. I am getting all but the first row currently with

$("#id").find("tr:gt(0)")

i need to combine this with not("tr:last") somehow maybe?

Jquery Solutions


Solution 1 - Jquery

Drop the gt(), as I'd assume it's a tiny bit slower than :first.

Use not() in conjunction with :first and :last:

$('table#tbl > tbody > tr').not(':first').not(':last').addClass('highlight');

Most browsers automatically add an tbody element in the table markup if that's missing, that is why the immediate children selector was failing – there were no <tr> elements as an immediate children to the <table> tag.

I am not 100% sure this is the way all browsers do it, so it would be safer to just add the <tbody> manually. Otherwise you need a little sniffing and cannot do it as an one liner:

if($('table#tbl > tbody').size() > 0) {
    $('table#tbl > tbody > tr').not(':first').not(':last').addClass('highlight');
} else {
    $('table#tbl > tr').not(':first').not(':last').addClass('highlight');
}

Hope this solves your problem!

Solution 2 - Jquery

Try this:

.not(':first').not(':last')

Solution 3 - Jquery

why not just this?

$('table tr:not(:first-child):not(:last-child)');

works as pure CSS selector as well.

Solution 4 - Jquery

You can combine the .not() methods into one by separating the selectors with commas:

$('#id tr').not(':first, :last');
$('#id tr:not(:first, :last');

Note that the second one is not valid in pure CSS, only as a jQuery selector. For pure CSS you'd have to use @Sumit's answer.

Solution 5 - Jquery

Strange the suggestions posted did not work, they should all work! BUT...

If it did not work, do it this way.... slightly slower but MUST WORK!! TRY:

$('table#tbl tr').addClass('highlight');
$('table#tbl tr:first-child').removeClass('highlight');
$('table#tbl tr:last-child').removeClass('highlight');

Solution 6 - Jquery

You can also use the .slice() method to remove the first/last elements from the set:

$('table tr').slice(1, -1);

The .slice() method essentially creates a new jQuery object with a subset of the elements specified in the initial set. In this case, it will exclude the elements with an index of 1 and -1, which are the first/last elements respectively.

###Example:

$('table tr').slice(1, -1).css('background-color', '#f00');

table { width: 100%; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr><td>1</td></tr><tr><td>2</td></tr>
  <tr><td>3</td></tr><tr><td>4</td></tr>
  <tr><td>5</td></tr><tr><td>6</td></tr>
</table>


Of course, you can also negate :first-child/:last-child using :not():

$('table tr:not(:first-child):not(:last-child)').css('background-color', '#f00');

table { width: 100%; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr><td>1</td></tr><tr><td>2</td></tr>
  <tr><td>3</td></tr><tr><td>4</td></tr>
  <tr><td>5</td></tr><tr><td>6</td></tr>
</table>


You can also combine :nth-child(n+2) and :nth-last-child(n+2):

$('table tr:nth-child(n+2):nth-last-child(n+2)').css('background-color', '#f00');

table { width: 100%; }

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr><td>1</td></tr><tr><td>2</td></tr>
  <tr><td>3</td></tr><tr><td>4</td></tr>
  <tr><td>5</td></tr><tr><td>6</td></tr>
</table>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser210757View Question on Stackoverflow
Solution 1 - JqueryTatu UlmanenView Answer on Stackoverflow
Solution 2 - JquerymikkelzView Answer on Stackoverflow
Solution 3 - JqueryProblemsOfSumitView Answer on Stackoverflow
Solution 4 - JqueryKatrinaView Answer on Stackoverflow
Solution 5 - JqueryStuart GView Answer on Stackoverflow
Solution 6 - JqueryJosh CrozierView Answer on Stackoverflow