Zach Ross-Clyne
Zach Ross-Clyne

Reputation: 799

jQuery - How to get height from child of class

I'm making a page with 2 div elements inside another:

<div id="id" class="class-name">
    <div class="child-1">

    </div>
    <div class="child-2">

    </div>
</div>

I'm trying this jQuery but it's not working:

$(window).load( function() 
{
    $('.class-name').each(function() 
    {
        var height = $(this).$('.child-1').height();
        $(this).$('.child-2').css('top', -height);
    });
});

Don't worry about class names, I've used cryptic ones here

Upvotes: 1

Views: 4108

Answers (3)

gokul
gokul

Reputation: 74

Try this,

<div id="parent">
<div class="div1"></div>
<div class="div2"></div></div>

CSS :
.div1{height:100px;} .div2{height:180px;}

$("#parent > div").each(function(index,element){
    var classnmae=$(element).attr("class");

    var height=$("."+classnmae).height();

});

Upvotes: 0

Nils
Nils

Reputation: 38

I think you should write it like:

$(window).load( function() 
{
    $('.class-name').each(function() 
    {
        var height = $('.child-1',this).height();
        $('.child-2',this).css('top', -height);
    });
});

Hope it helps!

Upvotes: 0

James Donnelly
James Donnelly

Reputation: 128781

Use jQuery's find() or children() methods:

var height = $(this).find('.child-1').height();
$(this).find('.child-2').css('top', -height);

Or:

var height = $(this).children('.child-1').height();
$(this).children('.child-2').css('top', -height);

Upvotes: 4

Related Questions