Need help with height() function and more...

Discussion in 'jQuery' started by absolute_beginner, Aug 27, 2012.

  1. #1
    Hello,

    I have to measure, and add, the eight of each item (<li>) of a list (<ul>) and when the sum comes to 200px (or more) put all next <li> into a new list...

    I've tried this :

    $(document).ready(function() {
    $('.list').each(function() {
    h=0;
    $(this).find('li').each(function() {
    h = h + $(this).height();
    if ( h >= 200 ){
    $(this).nextAll().insertAfter($(this).parent('.list')).wrapAll('<ul class="list second_column"></ul>');
    $(this).parent('.list').css("height", "200px");
    h=0;
    }
    })
    })
    });


    It doesn't work, '$(this).height()' isn't returning the <li> height, it returns 0...

    I'm very knew to it...
    any help would be appreciated.

    Thanks a lot
     
    absolute_beginner, Aug 27, 2012 IP
  2. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #2
    $.height has a tendancy to be a bit crap in certain browsers. I would use $(this).css("height"); instead.

    
    $(document).ready(function()
    {
         var numLists = 1;
    
         $('.list').each(function()
         {
             var h = 0;
             $(this).find('li').each(function()
             {
                 h += $(this).css("height");
                 if(h > 199)
                 {
                     //Im not 100% sure what your trying to do here but I think it can easily be cleaned up
                     //$(this).nextAll().insertAfter($(this).parent('.list')).wrapAll('<ul class="list second_column"></ul>');
                     
                     numLists++;
                     $(<.list parent>).append('<ul id="'+numLists'" class="list second_column"></ul>');
                     $("#"+numLists).append($(this));
                     $(this).parent('.list').css("height", "200px");
                     h = 0;
                 }
             });
         });
    });
    
    Code (markup):
    I hope that helps.

    Thanks
    Andrew
     
    awood969, Sep 19, 2012 IP
  3. awood969

    awood969 Member

    Messages:
    186
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    40
    #3
    Just to add the <.list parent> is not some crazy jQuery code, just me for whatever that selector is id'/class'd.

    Thanks
    Andrew
     
    awood969, Sep 19, 2012 IP