Dilkush Dilkush - 6 months ago 12
jQuery Question

How to find parent node of a row using jquery

From following html

<tbody>
<tr id="article0">
<td id="CheckboxDiv0">
<div class="checkbox">
<label>
<input type="checkbox" id="CheckboxArticle0" value="helo">
</label>
</div>
</td>
</tr>
</tbody>


I am trying to get the parent id

$ParentId = $("#checkboxArticle0").parentNode.parentNode.parentNode.parentNode.id;


The code is not working fine. Can someone suggest me how to grab the id of the parent div which is "article0"?

Answer

You can use .closest()

var parentId = $("#CheckboxArticle0").closest("tr").attr("id");

Description:

The closest() method returns the first ancestor of the selected element.
Refer: This for more.

Comments