In the code below I assign the gobal variable "myDiv" a jQuery element. But then inside the "test()" function it seems the myDiv variable is declared but not defined. I have to reassign it $('myDiv') again before I can do anything with it.
The Bogus variable xyz seems to be reachable just fine though...
<!DOCTYPE html>
<htm>
<head>
<title>Javascript Test Code</title>
<script src="js/jquery-1.9.0.min.js"></script>
<script>
var App = App || (function () {
var myDiv = $('#myDiv'), // define a global jQuery element variable.
xyz = 'xyz'; // and a bogus variable as a test.
function test()
{
console.log('running test...','myDiv is', myDiv); // length is 0
console.log('xyz is ', xyz); // says xyz
myDiv = $('#myDiv'); // after selecting with jQuery...
console.log('running test...','myDiv is', myDiv); //length is 1
console.log('xyz is ', xyz); // still says xyz
}
return {test:test}
}());
$(function(){
App.test();
});
</script>
</head>
<body>
<div id="myDiv"></div>
</body>
</html>