your aren't returning anthing from the first function here is one way to do it by taking randomString as global variable http://jsfiddle.net/venkatjp/GFK6B/2/.
return randomstring;
in first function and randomstring = randomNumber(randomstring);
in click event.
Scope within several functions Javascript
-
11-07-2023 - |
Domanda
I am trying to produce a variable with one function and use it in another function, and then pass it again to another function from the second function. I have tried placing the variable inside the parenthesis in a multitude of ways but i cannot seem to get the variable to alert in the final function. JSfiddle
function randomNumber(randomstring) {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 16;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
//document.randform.randomfield.value = randomstring;
// alert(randomstring);
}
$("#btn").click( function() {
randomNumber();
//alert(randomstring);
update();
});
function update() {
alert(randomstring);
}
I have gone through several documents on Scope but i cannot see what i am not quite doing right here.
Soluzione 5
Altri suggerimenti
A variable declared within a function, with var
statement, is visible only within that function.
So, you can return the randomString
and pass that around, like this
function randomNumber(randomstring) {
...
...
return randomstring;
}
$("#btn").click( function() {
update(randomNumber());
});
function update(randomstring) {
alert(randomstring);
}
You have to declare it in a scope that contains all the functions that use it.
var randomstring; // e.g. here
function randomNumber(randomstring) {
randomstring = ''; // No var here. Use the existing one, not a new one in local scope
The randomstring
variable is with the randomNumber scope. So you have a few options on how to allow the update function to use it.
- Make it a Global Variable - This is the worst solution.
Rewrite the Update Function - Call the update function with the randomString as a parameter like so:
function update(randomstring) { alert(randomstring); }
Create an Object - Wrapping both functions in an object with randomstring being a property of said object. Here is an example:
JSFiddle: http://jsfiddle.net/GFK6B/3/
var obj = {
randomNumber: function() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 16;
var randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
this.randomstring = randomstring
},
update: function() {
alert(this.randomstring)
}
}
$("#btn").click(function() {
obj.randomNumber()
obj.update()
})
define var randomstring = '';
in global
name space, not in randomNumber
function
JS
var randomstring = '';
function randomNumber() {
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 16;
randomstring = '';
for (var i=0; i<string_length; i++) {
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum,rnum+1);
}
}
$("#btn").click( function() {
randomNumber();
update();
});
function update() {
alert(randomstring);
}