Search code examples
javascriptjqueryhtmljquery-ui-resizable

How to use live width of element to resize another div at the same time?


I have a special task, I need to resize a div whith the live width of the another div being resized, I'd like to know how I can do that, see Fiddle for example : http://jsfiddle.net/Lindow/x8tdp0kg/14/, (click on a text div to make the handles appear). I tried using width but it doesn't work on my code.

Javascript :

$('#handles_manager').resizable({
  handles: {
    'e': '#egrip',
    'w': '#wgrip'
  }
});

function showHandles(e) {
    var width = $(this).width();
    var pos_left = $(this).position().left;
    var post_top = $(this).position().top;
    $('#handles_manager').css({'width':width, 'transform':'translate3d(' + pos_left + 'px, ' + post_top + 'px, 0px)', 'visibility':'visible'});

    var handles_width = $('#handles_manager').width(); // Get live width
    $(this).css({'width': handles_width + 'px !important'});

}

var elements = document.getElementsByClassName('element');
for(var i = 0; i < elements.length; i++) {
    var element= elements[i];
    element.addEventListener('click', showHandles);
}

HTML :

<div id="elements">
  <div class='element' style="transform: translate3d(0px, 0px ,0px)">
    <div class="inner-text">sss ss I'mfdgfdg fdgdfgfdgtryin sjdsaidj sdk dasd k sadopskaf kpsoadk ijfdk jod sadsassads</div>
  </div>

  <div class='element' style="transform: translate3d(0px, 100px ,0px)">
    <div class="inner-text">sss ss I'mfdgfdg fdgdfgfdgskaf kpsoadk ijfdk jod sadsassads</div>
  </div>
</div>

<div id="handles_manager">
  <div id="resize_content"></div>
  <div class="ui-resizable-handle ui-resizable-e" id="egrip"></div>
  <div class="ui-resizable-handle ui-resizable-w" id="wgrip"></div>
</div>

There are reasons I'm using this method, that's why I'm not entering the handles directly into the html code.

Any suggestion ?


Solution

  • Found the solution, I just had to use $('#...').resize(function(e) {} to be able to make live changes.