0

I have prompt dialog box which take Email value and need to pass the Email variable in another function Email(). Here is my code.

function promptMessage() {
    public var Email = prompt("Enter your Email :", "");
}
function Email(){
    alert("Email Address is " +Email); //Email from promptMessage() function 
}
4
  • 1
    declare Email in global scope.. Commented Feb 4, 2013 at 4:58
  • either make it a global variable or have a helper function take a parameter that you can pass it to. Commented Feb 4, 2013 at 5:10
  • What is public doing there? Commented Feb 4, 2013 at 5:16
  • possible duplicate of Using javascript variables from another function Commented Feb 4, 2013 at 5:18

3 Answers 3

3

How about

function promptMessage() {
    return prompt("Enter your Email :", "");
}
function Email(email){
    alert("Email Address is " +email); //Email from promptMessage() function 
}
Email(promptMessage());
Sign up to request clarification or add additional context in comments.

Comments

1

Just declare it outside of the scope of the functions and dont use a function name as a variable name this will leed to nightmares:

var EmailAddr;

function promptMessage() {
     EmailAddr = prompt("Enter your Email :", "");
}
function Email(){
    alert("Email Address is " +EmailAddr); //Email from promptMessage() function 
}

Comments

0

Since you are declaring the Email variable inside a function(in local scope), that's why it is not accessible outside of this function. Instead, define it outside like this as a global var :

var email;
function promptMessage() {
    email = prompt("Enter your Email :", "");
}
function Email(){
    alert("Email Address is " +email); //Email from promptMessage() function 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.