I have the following code that I am going through the tables columns and if its the last column I want it to do something different. Right now its hard coded but how can I change so it automatically knows its the last column
$(this).find('td').each(function (i) {
if(i > 0) //this one is fine..first column
{
if(i < 4) // hard coded..I want this to change
{
storageVAR += $(this).find('.'+classTD).val()+',';
}
else
{
storageVAR += $(this).find('.'+classTD).val();
}
}
});
To break a $. each or $(selector). each loop, you have to return false in the loop callback. Returning true skips to the next iteration, equivalent to a continue in a normal loop.
each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.
The . each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0.
If you want access to the length inside the .each()
callback, then you just need to get the length beforehand so it's available in your scope.
var cells = $(this).find('td');
var length = cells.length;
cells.each(function(i) {
// you can refer to length now
});
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