HTMLTableCellElement: cellIndex property

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

The cellIndex read-only property of the HTMLTableCellElement interface represents the position of a cell within its row (). The first cell has an index of 0.

Value

Returns the index of the cell, or -1 if the cell is not part of any row.

Examples

This example adds a label to all the cell numbers of the first row of the tbody.

HTML

html
Item Price
Bananas $2
Rice $2.5

JavaScript

js
const rows = document.querySelectorAll("tbody tr");
const cells = rows[0].cells;

for (const cell of cells) {
  cell.textContent = `${cell.textContent} (cell #${cell.cellIndex})`;
}

Result

Specifications

Specification
HTML
# dom-tdth-cellindex

Browser compatibility