Pass multiple value inputs from js file to another js file

Viewed 30

I have 3 files - main.html, one.js and two.js.

Here's a code snippet inside main.html:

<html>
    <head>   
       <head>
          <script type="text/javascript" src="one.js"></script>
          <script type="text/javascript" src="two.js"></script>
       </head>
       <body>
          <input type="text" id="txtOrigin" name="txtFrom"/>
          <input type="text" id="txtDestination" name="txtTo"/>
          .....
          <button type="submit" id="buttonBusSearch" >Search Bus></button>
       </body>
</html>

and inside one.js:

function load()
{
   jq("#txtOrigin").val(txtFrom);
   jq("#txtDestination").val(txtTo);
   jq("#txtOrigin").attr("state", stateFrom);
   jq("#txtDestination").attr("state", stateTo);
   ....
   jq("#buttonBusSearch").click();
   //need to pass all these val to function test()
}

jq(document).ready(function () 
{
   jq("#buttonBusSearch").click(function () 
   {
       ....
   }
}

Here is two.js:

function test()
{
   console.log(); //to check whether the values has been passed or not
}

I would like to pass txtFrom, txtTo.. values to two.js. Note that these codes were not originally written by me and I need to modify the code inside two.js only. Is there other way to pass these multiple values instead of using global variable? Thanks in advance.

1 Answers

Since the two files are in the same document, there is no need to "pass" anything from one file to the other.

jq("#buttonBusSearch").click(function() { test(txtFrom,txtTo)}); 

will work and so will

function test() {
  console.log(jq("#txtOrigin").val(),jq("#txtDestination").val())
}
Related