how get dynamic div width in javaScript

2.7k views Asked by At

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.

1

There are 1 answers

10
k-nut On BEST ANSWER

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/