Vicky Siddharth
Vicky Siddharth

Reputation: 127

how to get parent div by using children div in jQuery?

in this i want div1 as answer...with the children divTab1Btn

am using

$('#divTab1Btn').closest().attr("id");

But am getting undefined ? what is the correct solution ?

<div id="div1">
    <div id="divTab1Heading" style="background-color: rgb(246, 246, 246);">
        <table style="width: 1024px; height: 40px;" border="0" cellspacing="0" cellpadding="0">
            <tbody>
                <tr>
                    <td align="left" style="width: 911px; float: left;">
                        <div disabled="disabled" class="tabBtn" id="divTab1Btn" style="display: block; visibility: visible;">
                            <input class="btneditclick" id="divTab1BtnEdit" style="width: 75px; display: block; visibility: visible;" type="button" value="Edit" table2focus="tblAddtype" classvalue="1">
                            <input class="btnconfirm" id="divTab1BtnConfirm" style="width: 85px; display: none; visibility: hidden;" type="button" value="Confirm" classvalue="1" hdnheadergridlinkfield="ADD_MRECID" gridid="1655">
                        </div>
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</div>

Upvotes: 1

Views: 3305

Answers (3)

Harshit Gupta
Harshit Gupta

Reputation: 739

You can use parent():

var obj = $("#divTab1Btn").parent();

Now you have object of parent div and you can use .attr() function to get attribute of parent div.

Example:

var obj = $("#divTab1Btn").parent();
var parentId = $(obj).attr("id");

Upvotes: 0

Brijesh Bhatt
Brijesh Bhatt

Reputation: 3830

Use .parents() to get all the parents and .last() will select the last parent element in all divs.

var id=$('#divTab1Btn').parents('div').last().prop("id");

Upvotes: 0

Arun P Johny
Arun P Johny

Reputation: 388316

The closest() method requires a selector to find the target element, else it will return an empty object that is the reason for the undefined value.

In your case since there are no other selector to find the parent, I think you can do something like get the table and then get the ID of its parent's parent.

$('#divTab1Btn').closest('table').parent().parent().attr("id");

Upvotes: 2

Related Questions