Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Javascript function from another JS file

Yes, I have both functions included in html. I know the ordering matters. What I'm confused about is the way that the JS functions are set up, and I don't know the right way to call the function I want.

For example, I have a Items.js, in which I show some things on the screen, but I want to hide all of those items when the user activates something in a Phone.js

How Items.js is set up:

Items = function()
{
    this.stop = function()
    {
       // Items are hidden
       $(this.ButtonDiv).hide();
       $(this.CounterDiv).hide();
    }
}

Now how do I call the stop function from Phone.js?

like image 475
Briz Avatar asked Jan 20 '23 05:01

Briz


2 Answers

Rather that declaring Items as a function try this:

var Items = {
    stop: function() {
        // Items are hidden
       $(this.ButtonDiv).hide();
       $(this.CounterDiv).hide();
    }
}

And call the function like: Items.stop();

like image 153
Ram Avatar answered Feb 01 '23 03:02

Ram


Items.js must be loaded first. Inside Phone.js you can call the function as:

Items.stop();

If that doesn't work (though I think it should), create a class instance of Items() first and then call the stop() method:

var items = new Items();
items.stop();
like image 28
Michael Berkowski Avatar answered Feb 01 '23 03:02

Michael Berkowski