0% found this document useful (0 votes)
7 views

Pass a value from function to other functions in Javascript

Pass a value from function to other functions in Javascript

Uploaded by

drawdenohj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Pass a value from function to other functions in Javascript

Pass a value from function to other functions in Javascript

Uploaded by

drawdenohj
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Pass a value from function to other functions in Javascript

Asked 6 years, 5 months ago Modified 6 years, 5 months ago Viewed 1k times

I need to get a value from an HTML input in a javascript function. I can successfully get the value form the HTML but I
couldn't pass that from 1 function to other. And I need to pass that value to other function that is available on that javascript.
0 I tried this code I'm getting undefined.

This is my HTML code:

<input value="3232" id="fn">

This is my script with the main function, and I can get the value from the HTML:

var PageKey = function(){


var val = document.getElementById('demo').value
}

var fun1 = function(data){

var fun2 = function(data){

var fun3 = function(data){

This is what I tried:

var fun3 = function(data){


PageKey();
}

javascript function

Share Improve this question Follow edited Jul 11, 2018 at 23:05 asked Jul 11, 2018 at 21:13
Vahid Boreiri ANZR
3,438 1 20 34 ANZR 91 1 2 9

Possible duplicate of How to return values in javascript – Guillaume Georges Jul 11, 2018 at 21:40

3 Answers Sorted by: Highest score (default)

By clicking “Accept all cookies”, you agree Stack Exchange


can store cookies on your device and disclose information in
accordance with our Cookie Policy.

Accept all cookies Necessary cookies only

Customize settings
If you want to pass the value that you get in PageKey to some other function, you need to add a return statement to
PageKey .
2
var PageKey = function(){
return document.getElementById('demo').value;
}

Now you'd be able to do this:

var fun3 = function(data){


var demoValue = PageKey();
// and then do whatever with demoValue
}

Share Improve this answer Follow answered Jul 11, 2018 at 21:22
Ovid2020
181 10

The data parameter in fun1, fun2 functions should be holding the value of val – ANZR Jul 11, 2018 at 21:49

Setting a parameter to hold a value in a function expression doesn't make sense... the function expression is saying "I want this logic that
I'm calling fun1 to accept some input and do something with it (and then maybe return some output)". You would then pass an argument
( data , in this case) to it at runtime. That'd look something like fun1(fun3(PageKey())) , after you have added a return statement to
fun3 . Or, if you're sure you want a function to have a parameter stored with certain value, you would use bind : var boundFun1 =
fun1.bind(null, data); – Ovid2020 Jul 11, 2018 at 23:12

^^ but I don't think that's what you're trying to accomplish, since you could do that in a way simpler expression. What are you trying to
make happen, here, on a top level? I want to make sure I'm understanding the question – Ovid2020 Jul 11, 2018 at 23:13

PageKey() is just getting the value, but it does not return it. So, you need to add return val; to PageKey() . Then, in fun3,
you can set a variable to what's returned from PageKey() . So, you would end up with this:
2
var PageKey = function(){
var val = document.getElementById('demo').value;
return val;
}

var fun1 = function(data){

var fun2 = function(data){

var fun3 = function(data){


var val = PageKey();
console.log(val);
// prints out what PageKey got
fun1(val);
fun2(val);
}

By clicking “Accept all cookies”, you agree Stack Exchange


can store cookies on your device and disclose information in
Share Improve this answer Follow edited Jul 11, 2018 at 22:57 answered Jul 11, 2018 at 21:21
accordance with our Cookie Policy.
Ryan Z
2,785 2 9 21

this works fine. But the data parameter in fun1, fun2 functions should be holding the value of val – ANZR Jul 11, 2018 at 21:45

Ah, in that case we need to call fun1 and fun2 inside of fun3. I've updated my answer to reflect this. – Ryan Z Jul 11, 2018 at 22:57
You have to return the value

1 var PageKey = function(){


var val = document.getElementById('demo').value
return val;
}

And then

var fun3 = function(){


return PageKey();
}

Share Improve this answer Follow answered Jul 11, 2018 at 21:18
user6749601

By clicking “Accept all cookies”, you agree Stack Exchange


can store cookies on your device and disclose information in
accordance with our Cookie Policy.

You might also like