# What is the column index of a table cell

A peculiar but fascinating problem presented itself to me today. It was one of those; "oh yeah, I'd better look at that" kind of problems that you think you'll solve in a few minutes but eventually grow to take hours.

Given a cell in a HTML table, what is it's column index?

This seems like it's simple. You just count the cells preceding it. Fine. Works for 99% of cases. But what if the table looks like this?:

cell 1 | cell 2 | cell 3 | cell 4 | cell 5 | cell 6 | |

cell 7 | cell 8 | cell 9 | cell 10 | |||

cell 11 | cell 12 | cell 13 | ||||

My index? |
cell 15 |

Suddenly things are very complicated again. The last row really only contains two cells and the browser reports cell indexes 0 and 1 on those, respectively. Not the 3 and 4 I was hoping for.

My first thought was simply adding up the number of cells preceding this one which had a greater *row index* + *row span* than the *row index* of my subject cell. This doesn't work because of cells like #4 & #13.

Eventually I simply ended up with a brute force method that does the job. But is unfortunately neither fast nor elegant. But then, what code of mine is?

It's presented here in case anyone else needs to solve the same annoying problem. Or if anyone can suggest a nicer (or preferably a faster) method of doing this.