Dilkush Dilkush - 10 months ago 39
jQuery Question

How to find parent node of a row using jquery

From following html

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

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"?


You can use .closest()

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


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