user123
user123

Reputation: 5407

Get variable value from one JS function to another

In my page I get user fb_id in one of function function1() when user perform login action.

Later when user click on some button another function call takes place which calls function2(). I want to fetch value of fb_id in function2(). How can i do it?

structure is like below:

<html>
<head>
<script> 
function fuction2()
{
// some process
var id = fb_it; // Getting fb_it from function1()
}

</script>
</head>
<body>
<script>
function function1()
{
    var fb_it='xyz';
}
</script>

<button type="submit" onclick="function2()"> GetID </button>
</body>

Is it possible or not? If yes then how?

Upvotes: 1

Views: 320

Answers (3)

Ronak Patel
Ronak Patel

Reputation: 2610

function function1()
{
    var fb_it='xyz';
return fb_it;
}

function fuction2()
{
// some process
var id = function1(); // Getting fb_it from function1()
}

Upvotes: 1

infidel
infidel

Reputation: 452

why don’t you use global variables?

<script>
var x = 0;
function(){
x = 7;
}
function(){
x= 8;
}
</script>

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382092

No you can't directly access a local variable from an external scope.

What you can do is to store the variable externally :

function fuction2() {
    var id = fb_it; // Getting fb_it from function1()
}
var fb_it;
function function1() {
    fb_it='xyz';
}

Upvotes: 3

Related Questions