0

I need to get a value from an HTML input in a JavaScript function. I can successfully get the value form the HTML pass that value to other function that is available on that JavaScript.

This is my HTML code:

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

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
  return val;
}

This is what I tried. I can just call the PageKey function in fun3 to get the output, but the returned value of PageKey function should input to fun3 argument. Ex: appData argument should be holding the returned value.

var fun3 = function(appData){

}
3
  • 1
    Values for function arguments are given when calling a function, not when defining a function. Commented Jul 12, 2018 at 16:22
  • 1
    Given the above code, call fun3(PageKey()). Inside fun3, you can now use appData to refer to the returned value. Commented Jul 12, 2018 at 16:22
  • Please explain what your actual goal is here. What is the code inside fun3 going to look like? What's the point of putting it in a function? Why do you have an appData parameter in the first place if you're never going to use it for anything other than PageKey()? Commented Jul 12, 2018 at 17:14

2 Answers 2

2
function PageKey() {
   return document.getElementById('demo').value
}

var fun3 = function (appData) {
    ....
}

var func = fun3(PageKey());
Sign up to request clarification or add additional context in comments.

2 Comments

Is there a way to do call fun3(PageKey()); inside the fun3 itself or can I do it without calling the function.
@Ayb You can do it, but it's kind of pointless. No offense, but I don't think you 100% understand what the point of functions is in the first place. If you just want to run code, there's no point wrapping it in a function. Unless you want to run that code multiple times, or at a specific point later in time.
0

Another way of doing it, provided that you are not calling fun3 from elsewhere (similar to Chris answer):

var fun3 = function(){
     var appData= PageKey();
}
//scope should be kept it mind

Comments

Your Answer

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