How to count table cells in a row with JQuery?
Asked Answered
H

1

11

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.

Hokkaido answered 7/12, 2011 at 0:28 Comment(0)
S
29

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
Surly answered 7/12, 2011 at 0:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.