Hello, tell me, please, my tables are in a div. I need to click on the table in the alert to display in what id it is.

$("table").click(function(){alert(...)}) 

Thank you in advance.

  • $ ("table"). click (function () {alert ($ (this) .parent ();)}) is it? - Mike
  • @ myvzar, gives me [object Object] - zkolya
  • $ ("table"). click (function () {alert ($ (this) .parent (). attr ('id');)}) - Aydishka Diva ... - Mike

2 answers 2

If the tables are nested directly in divs, then

 $("table").click(function () { alert($(this).parent().attr('id')); }); 

http://jsfiddle.net/zbtj3s5b/6/

     $("table").click(function() { // Так мы ищем первый родительский div, у которого есть атрибут ID var parentDivId = $(this).closest('div[id]').attr('id'); alert(parentDivId) }); 
     table, table td { border: 1px solid #ccc; } div + div { margin-top: 10px; } 
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="first"> <div> <table> <tr> <td>Я столбец 1 в таблице 1</td> <td>Я столбец 2 в таблице 1</td> </tr> </table> </div> </div> <div id="second"> <table> <tr> <td>Я столбец 1 в таблице 2</td> <td>Я столбец 2 в таблице 2</td> </tr> </table> </div>