Jump to content
joymis

How can I get tr index

Recommended Posts

Hello,

I have some html code like this

<table>
  <thead>....</thead>
  <tbody id="content">
    <tr>
      <td>
      	<img ......>
        <img onclick="test()" src="1.jpg">
      </td>
    </tr>
    <tr>
      <td><img onclick="test()" src="3.jpg"></td>
    </tr>
    <tr>
      <td>
        <img ......>
        <img ......>
        <img onclick="test()" src="2.jpg">
      </td>
    </tr>
  </tbody>
</table>

when I onclick 3.jpg, I want get tr index is 1, and I onclick 1.jpg, I want get tr index is 0

how can I use jQuery or javascript get the tr index and no use like parentNode api?

because I not sure parentNode of img must be td, maybe it's div, span, or a table.

Thanks.

Share this post


Link to post
Share on other sites

You can also do this without jQuery using the rowIndex property of the table row element: https://developer.mozilla.org/en-US/docs/Web/API/HTMLTableRowElement/rowIndex

If you have a reference to the image, you can get to the table row using the parentNode property

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...