rahul
rahul

Reputation: 1122

Parent of an id using Jquery

<div class="cdlButton" style="width:40px; float: left;">  // want this div element
     <div style="">
         <a id="changestatus" class="" onclick="changeWorkflowStatus()" href="#">
             <img id="" width="32px" height="32px" title="disable" alt="disable"  src="images/disable.png">
        </a>
    </div>
</div>

I want div with help of jquery Right now i am doing this way

 $("#changestatus ").parent().parent().addClass('disablecdlButton');

Is there any other way to get top div element

Upvotes: 2

Views: 102

Answers (6)

Satpal
Satpal

Reputation: 133403

You can try

$("#changestatus ").parents(".cdlButton").addClass('disablecdlButton');

Upvotes: 1

SarathSprakash
SarathSprakash

Reputation: 4624

DEMO

Try this, .parents('div').last() will select the top most parent div element of the selected element , here it is changestatus

$("#changestatus ").parents('div').last().addClass('disablecdlButton');

Hope this helps,Thank you

Upvotes: 0

Manu M
Manu M

Reputation: 1064

 $("#changestatus ").parents('div.cdlButton').addClass('disablecdlButton');

Upvotes: 0

Rob Schmuecker
Rob Schmuecker

Reputation: 8954

You can do various things here

$(".cdlButton").addClass('disablecdlButton');
$('#changestatus').closest('div.cdlButton').addClass('disablecdlButton');
$($('#changestatus').parents().get(-1)).addClass('disablecdlButton');

Upvotes: 2

Dipesh Parmar
Dipesh Parmar

Reputation: 27354

$('#changestatus').closest('div.cdlButton').addClass('disablecdlButton');

For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

Official Document

Upvotes: 7

Suresh Atta
Suresh Atta

Reputation: 121998

use closest()

Description: For each element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree.

$("#changestatus").closest("div.cdlButton").addClass('disablecdlButton');

Upvotes: 1

Related Questions