2

I have the following html code. I create an Object of Customer. (the constractor in custForATM.js) :

<html>
    <script src="js/custForATM.js"></script>
    <script src="js/ATM.js"></script>   
    <script type="text/javascript">
        var customer = new CustomersATM("300321758","1234","Eric");
    </script>
<body>
    <button onclick="changeID()">Add One to Customer ID</button>
</body>
</html>

The function changeID() located in diffrent JS file (ATM.js) . I want that click on this button will add send the Variable "customer" to the changeID function. Is it possible? (I know that I can move this method to the html file. but I don't want.

Thanks!!

Blundering Philosopher
  • 6,245
  • 2
  • 43
  • 59
user2970484
  • 89
  • 1
  • 5
  • 12

4 Answers4

5

In your example, customer is a global variable. You should be able to do the following:

<button onclick="changeID(customer)">Add One to Customer ID</button>
Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
  • Thanks! and if I change the 'customer' in the function. (add 1 to id) it will change here too? or only in the function changeID scope. Thanks!! – user2970484 Jan 04 '14 at 23:12
  • the `customer` variable is global for everywhere, in every JS files – Reza Mamun Jan 04 '14 at 23:14
  • customer is an object, and it thus passed by reference to the changeID function. This means that any properties you change on customer will be changed anywhere you use the variable. – Alexander O'Mara Jan 04 '14 at 23:15
  • @user2970484 I don't recommend this answer. Please read some of these results https://www.google.com/search?q=Why+is+inline+js+bad%3F and check out my answer to follow best practices. – m59 Jan 04 '14 at 23:26
3

Within the global scope (aka "window"), variables are global.

Check this out:

//this is global because it is in the global scope (the window)
var foo = 'stuff';
//because it is global (window) you can also access it like this:
console.log(window.foo); //'stuff'

Now you can access foo anywhere. It's worth noting that globals aren't best practice - so check out Object Oriented Programming (SOLID principles).

If you are within another scope (like a function) and don't use the var keyword to create a variable, the variable will be global:

function someFunction() {
  someVariable = 'stuff'; //this created a global variable! (or references an existing one)
  //you could also assign it to window:
  window.someVariable = 'stuff';
  //both are the same thing!
}

Inline js (onclick in your html) is not a good practice. Instead, you can follow the good practice and use javascript to register the click event:

//get reference to button
var myBtn = document.getElementById('myBtn');

//add click function
myBtn.addEventListener('click', function(event) {
  myFunction();
});

function myFunction() {
  console.log(foo); //'stuff' 
}

Here's a demo of all of this: http://jsbin.com/OmUBECaw/1/edit

Just note that you'll need to get the element references after they are loaded into the dom. It's best practice to include your scripts just before the end of the body rather than in the head, like this:

  <!-- scripts here! -->
  <script></script>
</body>

If you must keep the scripts in the head, then you'll need to put your javascript code in a function to run onload of the window:

window.addEventListener('load', function() {
  //code here!
});
m59
  • 43,214
  • 14
  • 119
  • 136
2

You can simply inline this. But as your logic becomes more complex it is going to eventually make sense to break the event handling into the head script as well.

You can tie into the window.onload event, and then locate your button element (preferably by using an id on the element), and then tie an onlcick event which will send the parameter.

<html>
<script src="js/custForATM.js"></script>
<script src="js/ATM.js"></script>   
<script type="text/javascript">
    var customer = new CustomersATM("300321758","1234","Eric");
    window.onload = function(){
     document.getElementById("changeId").onclick = function(){
      changeID(customer);
     };
    };
</script>
<body>
 <button id="changeId">Add One to Customer ID</button>
</body>
</html>
Travis J
  • 81,153
  • 41
  • 202
  • 273
-4

This should work. (Removing "var" makes variable global)

<html>
<script src="js/custForATM.js"></script>
<script src="js/ATM.js"></script>   

<script type="text/javascript">

customer = new CustomersATM("300321758","1234","Eric");

 </script>
 <body>

<button onclick="changeID(customer)">Add One to Customer ID</button>
</body>
</html>
Jurion
  • 1,230
  • 11
  • 18