2

I have the following:

var thisTable = $(this).closest('table').id;

Firebug shows:

$(this).closest('table').id -> Undefined

However, the following does work:

$(this).closest('table').attr('id')  -> "myTable"
$(this).closest('table').prop('id')  -> "myTable"

Is the .id method not supported after jQuery's selectors like .closest?

gene b.
  • 10,512
  • 21
  • 115
  • 227

1 Answers1

4

.id is not method, but property. jQuery does not have it, js has. So you need to do $(this).closest('table')[0] to get js object, than $(this).closest('table')[0].id

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="id-of-tbl">
  <tr>
    <td><button onClick="console.log($(this).closest('table')[0].id)">Get ID</button></td>
  </tr>
</table>
Justinas
  • 41,402
  • 5
  • 66
  • 96