techlead
techlead

Reputation: 779

Check if the sibling of an element has a child that is a checkbox in jquery

HTML Code:

<div>
    <div>
        <input id="chkA" type="checkbox" name="chkA">           
    </div>                                                  
    <div>A</div>
    <div id="A"></div>
</div>

Context is:

<div id="A"></div>

Problem:

I want to navigate to the first sibling of <div id="A"></div> and check if it has a child of type input/checkbox.

Upvotes: 0

Views: 2095

Answers (3)

Drafter
Drafter

Reputation: 13

if($('#A').prevAll().has(':checkbox').length>0) {
//code
}

Upvotes: 0

ShankarSangoli
ShankarSangoli

Reputation: 69915

Try this

If you know the sibling is below the element

if($("#A").next().chldren(":checkbox").length){
     //Yes I am a checkbox
}

Or if it is above

if($("#A").prev().chldren(":checkbox").length){
     //Yes I am a checkbox
}

Or if you dont know

if($("#A").siblings(":first").chldren(":checkbox").length){
     //Yes I am a checkbox
}

Upvotes: 1

Dennis
Dennis

Reputation: 32608

 if( $("#A").siblings().first().children("input[type='checkbox']").length > 0) {
    //code
 }

Upvotes: 3

Related Questions