Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cells (<td>) x and y coordinate in table using jQuery?

I'm looking for a good way to get cells X-Y position in a table. (do not confuse it with css-position, I am looking for X and Y coordinate in Cartesian coordinate system).

As we know, we can get particular cell in a table using ex: $('#grid')[0].rows[5].cells[7].

But, what if I want to get the value 5x7 when I click on particular cell, i.e:

$('#grid td').click(function(){
   alert("My position in table is: " + myPosition.X + "x" + myPosition.Y);
});

I guess, the easiest way is to add innerHTML, ID or CSS class to every cell (<td class="p-5x7"> etc.) when creating table in back end, then parse it, and return on click, but is there any way to get these coordinates based purely on DOM?

like image 791
rochal Avatar asked Nov 21 '09 13:11

rochal


People also ask

How can get TD value from table in jQuery?

jQuery: code to get TD text value on button click. text() method we get the TD value (table cell value). So our code to get table td text value looks like as written below. $(document). ready(function(){ // code to read selected table row cell data (values).

How do you traverse a table in jQuery?

The jQuery traversing methods allow you to iterate DOM elements in a DOM hierarchy. Use the selector to get the reference of an element(s) and then call jQuery traversing methods to edit it. Important DOM manipulation methods: each(), children(), find(), first(), parent(), next(), previous(), siblings() etc.

How can find HTML table using jQuery?

Search all columnsThe first textbox use to search value on all columns. When keyup event triggers then hide all <tbody><tr> and search value on all <td> of <tbody><tr> . If value found then show the <tr> . Display <tr class='notfound' > if not record found.


1 Answers

DOM Level 2 HTML cellIndex:

alert('My position in table is: '+this.cellIndex+'x'+this.parentNode.rowIndex);
like image 125
bobince Avatar answered Oct 07 '22 16:10

bobince