I have one variable in function, I want to access it from other function. I cannot define this variable out of function. I set the example code for review. http://jsfiddle.net/VC6Wq/1/
function one(){
var zero= 500;
}
function two(){
alert(zero)
}
I have one variable in function, I want to access it from other function. I cannot define this variable out of function. I set the example code for review. http://jsfiddle.net/VC6Wq/1/
function one(){
var zero= 500;
}
function two(){
alert(zero)
}
Try this :
function one(){
var zero = 500;
return zero;
}
function two(){
alert(one());
}
two();
Or define any other variable globally and assign it the value of the 'zero' :
var zero_2;
function one(){
var zero = 500;
var zero_2 = zero;
}
function two(){
alert(zero_2);
}
two();
Since you cannot define the variable globally, one approach is simulate a global variable, attaching it to a DOM node, like this:
//note the notation 'data-'
<div id="node" data-zero='500'/>
To set the value:
// Store the value into data var within the div
$('#node').data('zero', value);
To get value:
var value = $('#node').data('zero');
alert(value);
Example: http://jsfiddle.net/VC6Wq/4/
two? Why you can not define it globally?data()like<div id="node" data-zero='myvalue'/> method. Then accessing to its value using$('#node').data('zero');