Skip to content Skip to sidebar Skip to footer

How To Find Whether Current TD Is Last TD In TR

I have a single row and multiple s in it. In one of my functions I come across a situation where I have to find out whether my currentSelectedTD is the last in

Solution 1:

You can use .next() to check if the element has the immediately following sibling.

if( $(currentSelectedTD).next().length == 0) {
    // is last 
}

Solution 2:

Think you should do :last filter on the full set and store the result in a variable. Then do a loop through the set and compare each one with the one you stored.


Solution 3:

use-

if($('.currentSelectedTD:last')){
//do what ever you want
}

or

$lastTd = $('.currentSelectedTD').parent('tr').find('td:last')

Solution 4:

You can check for this quite easily, and without tapping into jQuery:

var isLastChild = currentSelectedTD.parentNode.lastElementChild == currentSelectedTD;

This has the advantage of being both native DOM and simple, however it would only work on a modern browser (e.g. needs IE9). You can tweak it to work with anything, but then it wouldn't be an one-liner.


Solution 5:

You just need to use:

$(currentSelectedTD).is("td:last");

Post a Comment for "How To Find Whether Current TD Is Last TD In TR"