Brian
Brian

Reputation: 294

Get sister element using jQuery

Is there a way to get a next element from a current sister element? Sorry if confusing.

<div class="wrapper">
    <div class="paDiv">
       <div class="saDivOne">
       </div>
    </div>
    <div class="paDivTwo">
       <div class="saDivTwo">
       </div>
    </div>
</div>


<script>
$('.paDiv').each(function(){
    htmlContent = $(this).children().html();
    SisterDivContent = $(this).next('.paDivTwo').children().html();
    console.log(SisterDivContent);
})
</script>

Upvotes: 0

Views: 201

Answers (2)

Pixelomo
Pixelomo

Reputation: 6737

Try using .sibling:

$('.paDiv').sibling('paDivTwo').html()

Upvotes: 1

RGS
RGS

Reputation: 5211

$('.paDiv').each(function(){   
var SisterDivContent = $(this).parent().find('.saDivTwo').html();
alert(SisterDivContent);
});

You have to add some contents inside div class 'saDivTwo'. Otherwise you will get empty content only.

Demo:

http://jsfiddle.net/U8n7g/

Upvotes: 1

Related Questions