Search code examples
javascriptjquerycssresizablejquery-ui-resizable

how get dynamic div width in javaScript


I have two div demo1 and demo2 .I need if I drag demo1 left to right and right to left in that time demo2 width automatically adjust . I tried this

<div class="wrapper">
              <div class="demo"></div>
              <div class="demo2"></div>
</div> 
<STYLE>
.demo {
    float: left; 
    width: 50%;
    height:100%;
    background-color: red;
}
.demo2 { 
    float: left;
    background-color: black;
    width: 50%;
    height:100%;
}</STYLE>
<script type="text/javascript">
         (function(){

      maxWidth=$('.wrapper').width();
      $('.demo').resizable(function(e){

        var x = e.pageX;
        alert(x);

        $(this).width(x);
        $('.demo2').width(maxWidth-x);


    });
})()
 </script>

But when I drag demo1 left to right, demo2's width is not adjusting automatically . Please any one tell me what is wrong in my code.


Solution

  • I think you misunderstood the syntax. You can bind to the resize event to do what you want:

    (function () {
        var maxWidth = $('.wrapper').width();
        $('.demo').resizable({
            resize: function (e, ui) {
                var x = e.pageX;
                $(this).width(x);
                $('.demo2').width(maxWidth - x);;
            }
        });
    })();
    

    Demo: http://jsfiddle.net/rcsxj3xb/1/