2

This question might be duplicated, but I haven't found anything helpful.

Here's my snippet:

$("table tbody tr").hover(

  function() {
    var secondCell = $(this).children[1].textContent;

    //secondCell.someCode
  },

  function() {
    //some code
  }

);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <thead>
    <tr>
      <th>foo</th>
      <th>foo</th>
      <th>foo</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>bar</td>
      <td>bar</td>
      <td>bar</td>
    </tr>
  </tbody>
</table>

All I wanna do is: When a player hovers a row, it should alert them a message and that message has the second cell text.
Hope you get the idea, and thanks in advance.

Goran Stoyanov
  • 2,311
  • 1
  • 21
  • 31
PepsiGam3r
  • 302
  • 1
  • 4
  • 10

3 Answers3

3

There are several ways:

$( "tr td:nth-child(2)" )

$( "tr").children().eq(1)

$( "tr td").eq(1)

$( "tr td").filter(":nth-child(2)")

Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168
  • Thank you habibi, but you know, I wanna get the cell of the row that's already selected, using $(this), thanks anyway. – PepsiGam3r Jul 05 '15 at 05:12
0

In jquery, .children() is a function. So you need to call it before you can get an element out of the array. Have a look at the jquery .children() documentation.

You can use it like this: jsfiddle.

k212
  • 88
  • 7
  • Oh yeah, I saw this one, but when I typed it my IDE said that `children` isn't a function, it's an array, but when I tried it it worked. – PepsiGam3r Jul 05 '15 at 05:12
0

You can use following code also

$("table tbody tr").hover(

    function() {
        var secondCell = $(this).find("td:eq(1)").text();

        //secondCell.someCode
    },

    function() {
        //some code
    }

);
Hemal
  • 3,682
  • 1
  • 23
  • 54