149

How do I get the row and column number of the clicked table cell using jQuery, i.e.,

$("td").onClick(function(event){
   var row = ...
   var col = ...
});

6 Answers 6

227

You can use the Core/index function in a given context, for example you can check the index of the TD in it's parent TR to get the column number, and you can check the TR index on the Table, to get the row number:

$('td').click(function(){
  var col = $(this).parent().children().index($(this));
  var row = $(this).parent().parent().children().index($(this).parent());
  alert('Row: ' + row + ', Column: ' + col);
});

Check a running example here.

5
  • Why does it fail on column spans @grom? Commented Mar 4, 2011 at 13:35
  • @Forkrul Assail, @CMS It's fail with ROWSPAN's jsbin.com/eyeyu/1099/edit#preview Click on cell with text "12". Column index must be 4
    – Andrew D.
    Commented Apr 2, 2012 at 9:39
  • I have some datas in the variable "data".....is it possible to append this "data" into the table with this col and row value
    – shanish
    Commented Apr 28, 2012 at 6:04
  • 2
    ROWSPAN & COLSPAN work around: This makes the click work when SPAN is used jsbin.com/eyeyu/1307/edit Commented Oct 21, 2012 at 1:58
  • 11
    var row = $(this).closest('tr').index()
    – SpYk3HH
    Commented Mar 23, 2015 at 20:18
124
$('td').click(function() {
    var myCol = $(this).index();
    var $tr = $(this).closest('tr');
    var myRow = $tr.index();
});
0
37

Get COLUMN INDEX on click:

$(this).closest("td").index();

Get ROW INDEX on click:

$(this).closest("tr").index();
22

Off the top of my head, one way would be to grab all previous elements and count them.

$('td').click(function(){ 
    var colIndex = $(this).prevAll().length;
    var rowIndex = $(this).parent('tr').prevAll().length;
});
0
6

Can you output that data in the cells as you are creating the table?

so your table would look like this:

<table>
  <thead>...</thead>
  <tbody>
    <tr><td data-row='1' data-column='1'>value</td>
      <td data-row='1' data-column='2'>value</td>
      <td data-row='1' data-column='3'>value</td></tr>

  <tbody>
</table>

then it would be a simple matter

$("td").click(function(event) {
   var row = $(this).attr("data-row");
   var col = $(this).attr("data-col");
}
4
  • 1
    Which html spec do those attributes comply with? I can't seem to find them. Commented Apr 25, 2009 at 5:36
  • 5
    HTML spec 5, which allows for custom attributes starting with the prefix 'data-'. Commented Apr 25, 2009 at 15:32
  • even though they aren't in spec it won't break older browsers < HTML5 Commented Aug 24, 2011 at 4:45
  • 1
    You can access "data" fields just by calling: $(this).data('row');
    – WhiteAngel
    Commented Mar 10, 2015 at 14:30
0

its better to bind a click handler to the entire table and then use event.target to get the clicked TD. Thats all i can add to this as its 1:20am

2
  • While this might be a valuable hint to solve the problem, a good answer also demonstrates the solution. Please edit to provide example code to show what you mean. Alternatively, consider writing this as a comment instead. Commented May 16, 2017 at 12:26
  • 1
    thanks, I will consider revising this answer in another 10 years
    – mkoryak
    Commented Oct 16, 2018 at 18:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.