Selecting a cell in an HTML table with jQuery

I wanted to get (and set) a cell value in a table using jQuery, selecting it by row and column index (its x and y position in the table, if you like). Frustratingly, I couldn’t find a simple one-liner to do it: I could find code that gets all the cells, or get them by ID (but I don’t want to have to make explicit IDs for each cell of my table), or get all the values in a given column (close, and did lead me in the right direction).

So here’s two similar ways to select a given cell, where row and col are my x, y positions, and my_table is the ID of the table:

$(‘#my_table tr:eq(‘+row+’) td’).eq(col)

and

$(‘#my_table tbody tr:eq(‘+row+’) td:eq(‘+col+’)’)

 

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *