Using function arguments in JavaScript

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


var fun3 = function(appData){
  var val = PageKey();
  console.log(val);
}

This is what I tried. it gives me the output I want, but I want fun3 argument (appData) to hold the value. It should be able to access PageKey() from appData arguments How do I do that?

Something like:

var fun3 = function(appData){
var val = PageKey(appData);

3 answers

  • answered 2018-07-12 04:10 Mark Meyer

    If you want the data to be available in the argument to fun3() you need to call fun3() and pass in the data:

    function PageKey() {
      var val = "some value" // document.getElementById('demo').value
      fun3(val); // pass value to fun3
    }
    
    
    function fun3(appData) {
      console.log(appData);
    }
    
    PageKey()

  • answered 2018-07-12 04:11 Bhojendra Rauniyar

    Define PageKey function like this:

    var PageKey = function(inputEl){
      return inputEl.value;
    }
    

    Then you can call like this:

    var inputElement = document.getElementById('demo');
    fun3(inputElement);
    

  • answered 2018-07-12 04:17 Mamun

    Simply pass PageKey() as the argument to fun3() like:

    fun3(PageKey());
    

    var PageKey = function(){
      var val = document.getElementById('demo').value
      return val;
    }
    
    var fun3 = function(appData){
      //var val = PageKey();
      //console.log(val);
      console.log('appData: '+ appData);
    }
    
    fun3(PageKey());
    <input value="3232" id="demo" value="testValue">