I have something like this...
$( 'ul li' ).each( function( index ) { $( this ).append( ',' ); } );
I need to know what index will be for last element, so I can do like this...
if ( index !== lastIndex ) { $( this ).append( ',' ); } else { $( this ).append( ';' ); }
Any ideas, guys?
The lastIndexOf() method returns the last index at which a given element can be found in the array, or -1 if it is not present. The array is searched backwards, starting at fromIndex .
The lastIndexOf() method returns the index (position) of the last occurrence of a specified value in a string. The lastIndexOf() method searches the string from the end to the beginning. The lastIndexOf() method returns the index from the beginning (position 0).
The last() function is an inbuilt function in jQuery which is used to find the last element of the specified elements.
var total = $('ul li').length; $('ul li').each(function(index) { if (index === total - 1) { // this is the last one } });
var arr = $('.someClass'); arr.each(function(index, item) { var is_last_item = (index == (arr.length - 1)); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With