how can I find the parent id of the div inside the table? I have the following structure:
<div id="parent">
<table>
<tbody>
<tr>
<td>
<div id="child" >
</div>
</td>
</tr>
</tbody>
</table>
</div>
<script type="text/javascript">
$(document).ready(function()
{
var parent = $("#child").parent().attr("id");
alert(parent);
});
When the div is inside < td> parent is empty. When I move the div outside < td> it works fine. Can you help me how to find the div inside ?
thanks
You can use .closest(), it finds the closest parent which matches the specified selector. In your case:
var parentId = $('#child').parent().closest('div').attr('id');
Documentation: http://api.jquery.com/closest/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With