How to count table cells in a row with JQuery?

aygeta picture aygeta · Dec 7, 2011 · Viewed 22.5k times · Source

I have an image gallery with say 39 number of images. Two buttons prev and next and in the middle a counter. Images are inside a table and 6 of images are shown.
When the user presses next button 6 visible images go up and 6 others show.
I want to count number of visible images.
Example 6/39 when next is pressed there should be 12/39. If I do +6 on every click sometimes the last tr has less than 6 images and when the counter goes +6 it surpasses the number of all images. So i need to now the index of current tr being showed. Then I need to count the td's of that tr and put this number in the counter and increment.

Answer

user497849 picture user497849 · Dec 7, 2011

assuming that your table has id "mytable", you can use

$( "#mytable tr td" ).length

but, say you want to access row 2, then you use

$( "#mytable tr:nth-child(2) td" ).length

but if you want to count the "td"'s of the visible row, use

$( "#mytable tr:visible td" ).length