Get value from assigned variable that has been scoped from global variable

193
December 10, 2017, at 5:09 PM

I've come into a problem - I have some jQuery code that gets window height everytime the user resizes it. However, I need to pass that value to another part of the code that's not within the function; and unsurprisingly, the code doesn't recognize the usage of the winHeight outside the function. It looks something like this:

$(document).ready(function() {
    // Calls function when window is resized
    $(window).resize(getWinHeight);
    var winHeight;
    function getWinHeight() {
        winHeight = window.innerHeight; // I want to get the value of winHeight
    // When the button is clicked
    var button = document.getElementById("button_container");
    $(button).click(function() {
        $("#middle").css("display", "block");
        $("html, body").animate({ scrollTop: winHeight}, 500); // winHeight isn't recognized!!
   });
});

Here's a jsFiddle that might help you get a better picture of the problem.

Rent Charter Buses Company
READ ALSO
How to time mutliple CSS events simultaneously?

How to time mutliple CSS events simultaneously?

If anyone can get this simple example to work, I'd be super thankful!

231
How to store a DOMDocument (Node) object in Django?

How to store a DOMDocument (Node) object in Django?

I would like to store the text a user is highlighting when they click a buttonI first get the Range from a user's selection with window

175
click subscribe/unsubscribe button more than once without having refresh whole page

click subscribe/unsubscribe button more than once without having refresh whole page

I want users on my page to be able to subscribe and unsubscribe to each other as many times as they want without having to refresh whole page between each click

235
Get request data in jQuery's AJAX response

Get request data in jQuery's AJAX response

Let's say I make a Dictionary app, where I check if some word exists or notI have a text input and a button:

199