Tuesday, May 28, 2024
 Popular · Latest · Hot · Upcoming
90
rated 0 times [  97] [ 7]  / answers: 1 / hits: 17189  / 12 Years ago, thu, april 5, 2012, 12:00:00

On pageload I set a variable



$(document).ready(function() {
var inv_count = 3;
});


When I try to refer to that variable inside functions though, it doesn't work.



function blah(a,b) {
alert (inv_count);
}


Why is this? And how can I get around it?



(rookie here)


More From » jquery

 Answers
2

If you declare a variable inside a function, the variable name will be inaccessible outside the scope of that function. Move the declaration outside the function:



var inv_count;
$(document).ready(function() {
inv_count = 3;
});

[#86402] Wednesday, April 4, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradford

Total Points: 709
Total Questions: 117
Total Answers: 91

Location: Sao Tome and Principe
Member since Wed, Dec 21, 2022
1 Year ago
;