+ 1
Undefined when using prompt in a function
why when I use this I get 'ig is not defined'. How should I return values? function askData(){ var ig = prompt("Instagram:"); var fb = prompt("Facebook:"); var em = prompt("Email:"); } askData(); sendData(ig,fb,em);
5 Antworten
+ 3
You would firstly need to get the function to return the variables ig,fb and em so that you can use them outside of the askData() function.
function askData(){
var ig = prompt("Instagram:");
var fb = prompt("Facebook:");
var em = prompt("Email:");
return [ig,fb,em];
}
var data = askData();
alert(data);
+ 6
They are all undefined because the variable declarations are inside the function askData. They have local scope.
Declare the variables outside for them to have global scope so you can pass them as arguments to sendData.
+ 1
No problem. Shame it took so long to get the answer you were looking for! Good luck!
0
thank you Jonathan Pizarra what I wanted was to ask for that info inside another function in case the user didnt fill them out. how can I do it?
0
thanks Russ