2

I have following script:

var height = 0;
$('.scroller').each(function() {

    $(this).children('div').each(function() {

        if (height < $(this).height()) {
            height = $(this).height();
        }

    });
    $(this).css("height", height + "px");

});

What it does:

  • loop through each children div inside each .scroller div
  • height < $(this).height() = if height of the currently looped div is bigger than 0 it is setting the height of this div as new value of the height variable
  • this way is finding the highest div element in each .scroller div
  • than, the height (height of the highest element ) is set to .scroller div

The issue:

  • if any element inside first '.scroller' is higher than ANY element in the second .scroller it is setting the height (highest element in first .scroller) also to second scroller - example: http://jsfiddle.net/MrTest/7jAAG/20/

Is element in the first .scroller are not higher than elements in second .scroller everything works fine - example: http://jsfiddle.net/MrTest/7jAAG/16/

I think the problem is setting the height var - how can I reset this value to 0 BEFORE going to loop though second .scroller ?

3 Answers 3

5

I may be misunderstanding but could you not just do this:

$('.scroller').each(function() {
    var height = 0;
    $(this).children('div').each(function() {

        if (height < $(this).height()) {
            height = $(this).height();
        }

    });
    $(this).css("height", height + "px");

});

the height variable will then get reset for each instance of .scroller in the DOM that's looped through.

Sign up to request clarification or add additional context in comments.

Comments

2

I think this might be what you are after. http://jsfiddle.net/7jAAG/30/

Set height for each iteration of .scroller.

$('.scroller').each(function() {
    var height = 0;
    $(this).children('div').each(function() {

        if (height < $(this).height()) {
            height = $(this).height();
        }

    });
    $(this).css("height", height + "px");
});

Comments

0

Below is how to set height to 0 between each iteration. I don't know if the result is what you actually expect.

http://jsfiddle.net/7jAAG/24/

var height = 0;
$('.scroller').each(function() {

    $(this).children('div').each(function() {

        if (height < $(this).height()) {
            height = $(this).height();
        }

    });
    $(this).css("height", height + "px");
    height = 0;
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.