Search code examples
javascriptjqueryjquery-ui-resizable

How to get selected div width when resize browser


I want to get selected div width according to following algorithm:

  1. If user resize browser then get div width according to resize browser div.

  2. If user don't resize browser then get div width normally.

Here is my code

jQuery(document).ready(function($) {

    if ($(window).resize) {
        var width = $('#main_header .wrapper').width($(window).resize);
    }
    else {
        var width = $('#main_header .wrapper').width;
    }

});

My code don't work, Please, any one can help me solving this problem. Thanks


Solution

  • This little function will get the width of #main_header .wrapper and update it on resize:

    $(function() {
        var $wrapper = $('#main_header .wrapper');//the element we want to measure
        var wrapperWidth = $wrapper.width();//get its width    
        $wrapper.text(wrapperWidth);//dunno what you want to do with wrapperWidth but here im setting it as the text of .wrapper just so you can see the value changing
        //add resize listener
        $(window).resize(function() {
            wrapperWidth = $wrapper.width();//re-get the width
            $wrapper.text(wrapperWidth);//update the text value
        });
    });
    

    Here's a working demo: http://jsfiddle.net/5rZ4A/