Javascript global variable not working properly?

Posted by Fabian on Stack Overflow See other posts from Stack Overflow or by Fabian
Published on 2010-04-07T11:02:42Z Indexed on 2010/04/07 11:03 UTC
Read the original article Hit count: 174

Filed under:
|
|
|

My jQuery code:

$(document).ready(function() {
    chrome.extension.sendRequest({get: "height"}, function(response) {
        height = response.value;
    });

    $("#id").css("height", height+"px");
});

You don't have to be concerned about the chrome.extension.sendRequest(), basically it communicates with a background page to fetch the value for "height" from localStorage and stores the value in global variable height.

The problem lies in $("#id") not being assigned the height value. However if I were to modify it such that it is now:

$(document).click(function() {
    $("#id").css("height", height+"px");
});

it works. Any idea why?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery