user2127324
user2127324

Reputation: 17

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

Upvotes: 0

Views: 3438

Answers (2)

Moob
Moob

Reputation: 16184

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/

Upvotes: 1

Buck Doyle
Buck Doyle

Reputation: 6397

Your use of if is the problem. Try this:

jQuery(document).ready(function($) {
  $(window).resize(function() {
    alert("$('#main_header .wrapper').width());
  })
});

$(window).resize will always return true because resize is a jQuery function. This code instead watches window for a resize event and calls the inner function when it happens.

I didn’t fill in the inner function because I wasn’t clear on what you wanted to happen upon resize.

Upvotes: 1

Related Questions