Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery alternative to td.cellIndex

I've got some javascript code referencing

var cell = event.target
if (cell.cellIndex === 3) {
    ...
}

Is the cellIndex property safe to use?

What would be a good cross-browser alternative?

jQuery is allowed.

like image 268
Raynos Avatar asked Feb 11 '11 10:02

Raynos


1 Answers

cellIndex is supported by all major browsers[src] and is defined in DOM level 2[src]. Stick with that. In jQuery, you can use:

$('#myTD')[0].cellIndex;

jQuery's index() might also be optimized to make use of cellIndex where available, would also work, though it would be less efficient because it works by getting an array of the parent node's children and finding the index of the current element in that array.

like image 90
Andy E Avatar answered Sep 19 '22 10:09

Andy E