Search code examples
jquerywidthjquery-ui-resizable

get sum of width of previous elements until particular element


i am trying to make splittable row columns using jquery ui-resizable. so here i need to get sum of the width of previous divisions of which is resizing. below is what i tried. but here if(cells[0] === currentSpan[0]) is not working.. any helps please. And if i can get this sum of div widths without a each will be great..

HTML

<div class='row'>
   <div class='simpleSpan' style='width:50px'></div>
   <div class='simpleSpan' style='width:10px'></div>
   <div class='simpleSpan' style='width:20px'></div>//this one will be re-sizing
   <div class='simpleSpan' style='width:20px'></div>
</div>

JS

 var appendRow = $('.row');
 $(".report .simpleSpan").resizable({
    handles: 'e',
    minWidth: '50',
    maxWidth: '500',
    resize: function() {
        var totWidth = 0;
        var currentSpan = this;
        $(appendRow).each(function(x,span) 
        {
            var cells = $(".simpleSpan", span);
            for(iCounter=0;iCounter<cells.length;iCounter++)
            {
                if(cells[0] === currentSpan[0])
                    return false;
                totWidth = totWidth + cells.width();
            }
        });
        alert(totWidth);
    },


  });

Solution

  • Guess this'll help you http://jsfiddle.net/un353ab6/1/

    var widthAdd = 0;
    var lengthPreDiv = $('.get').prevAll().length;
    for(i=0; i<lengthPreDiv; i++)
    {
        widthAdd = widthAdd+parseInt($('.get').siblings().eq(i).width());
    }
    alert(widthAdd);