views:

13

answers:

1

How do I achieve it?

I have a module named "tooltip" which has a "fade" function which in turn uses a global "element" variable. That variable is a reference to an element of the DOM. I want to update it from another module named "lightbox" so I could just let the "fade" function handle the fade-in effect. All my modules are declared using a closure.

    var tooltip = function{
       var element;
       return{
          fade: function(){ fade code goes here...}
       };
    }();

Can I just do the following to update "element" from the lightbox module?

tooltip.element = document.getElementByID('lightbox-con');

No jQuery code pls...

+1  A: 

If with module you mean object then you can just do like this:

var tooltip = {
    element: null,
    fade: function() {
        //fade code goes here...
        // you can access the element via this.element
    }
}

then you can update the element as you described:

tooltip.element = document.getElementByID('lightbox-con');

But if element is only used in the fade function, you could also consider to just pass the element to that function:

var tooltip = {
    fade: function(element) {
       //fade code goes here...
    }
}

and do:

tooltip.fade(document.getElementByID('lightbox-con'));

It depends on what you actually want to do.

Felix Kling
I guess that's basically it. tooltip and lightbox are objects, each in a separate file. Just started learning so I am still finding my way around..
Joann
no element is a global variable... thanks!
Joann
@Joann: Not sure if I understand. In your original code, `element` is not a global variable. It is local to the anonymous function. You cannot change it the way you described with your original code.
Felix Kling
I see.. I am just a little confused right now..
Joann