1

I have an event listener in header:

window.onkeydown = function(e) {
    var key = e.keyCode ? e.keyCode : e.which;
    if(key == 27) {
        var panel = document.getElementById('largeImgPanel');
    hideMe(panel);
    }
    if(key == 39) {
        arrow_right.onclick = onRight; //Wrong
    }
};

Lower i have a function:

window.onload = function() {
           ...
    var onRight = function showNext(img_thumb) {
    index = index + 1;
    document.getElementById('largeImg').src = arr_big[index].src;
    showLargeImagePanel();
    unselectAll();
};
arrow_right.onclick = onRight;

My question is: How i can "execute" onRight variable from event listener?

Gudron Swiss
  • 445
  • 1
  • 4
  • 6
  • in order to have access to each other, the two would have to share scope - so figure out a way to share their scope by making one more accessible – Travis J Oct 16 '13 at 20:43
  • if i make the onRight variable global, there is mistake because half of information for this function is not loaded yet :/ – Gudron Swiss Oct 16 '13 at 20:46
  • so add `onRight` event listener in `onload` listener callback – lukaleli Oct 16 '13 at 20:47
  • @GudronSwiss - You don't need to worry about that because the information will not be gathered until called for if it is inside of the function. And it will not be called until the page is loaded. – Travis J Oct 16 '13 at 20:48

1 Answers1

0

Allow onRight to be a global variable. Although this is not always desirable, in this instance it will work. Keep in mind, it is best not to pollute the global namespace.

window.onRight = function showNext(img_thumb) {...

and then later you may access it the same way

arrow_right.onclick = window.onRight;
Community
  • 1
  • 1
Travis J
  • 81,153
  • 41
  • 202
  • 273