Search code examples
jqueryhtml-tableoffset

How can I get offset of td using jQuery


I have a simple table:

<tr>
    <td></td>
    <td></td>
</tr>
<tr>
    <td></td>
    <td></td>
</tr>

and I need to get offset of cell on which cursor is hovered on jQuery


Solution

  • Demo on Fiddle

    HTML:

    <table>
      <tr>
        <td>One</td>
        <td>Two</td>
      </tr>
      <tr>
        <td>Three</td>
        <td>Four</td>
      </tr>
    </table>
    

    jQuery:

    $('td').mouseover(function() {
        var ofst = $(this).offset();
        $('div').html('(' + ofst.top + ', ' + ofst.left + ')');
    });