0

I have this issue with this Javascript event listener. On click, it replaces an overlay, showing information depending on the details. (admin, player)

I want it to work in another way, whereas if you hold down shift, it copies the span textContent and does not replace the overlay. I've tried several ways for this to work, though none have done it as I want it to.

The issue seems to be with the seemingly unnecessary round function brackets at the end. }());

Why are they there in the first place, and why does the replaceOverlay function stop working when they are removed?

Their code:

span.on("click", function() {
    var player = players[i];
    return function() {
        Class.replaceOverlay(Class.AdminPlayerLookupOverlay, {
            admin: admin,
            player: player
        })
    }
}());

What I would expect would work, but doesn't. (event is undefined. Replaces overlay no matter if shiftkey is down):

span.on("click", function(event) {
    var player = players[i];
    if (typeof event !== "undefined" && event.shiftKey == true) {
        Class.copyText(event, span, playerName)
    }
    return function() {
        Class.replaceOverlay(Class.AdminPlayerLookupOverlay, {
            admin: admin,
            player: player
        })
    }
}());

How could I get this working?

Thanks in advance :)

Commander
  • 13
  • 1
  • 5
  • 1
    Take a look at [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE) – Yousaf Aug 05 '20 at 15:36
  • 2
    The function that *seems* to be the event handler really isn't. The outer function is called on the last line of your code; that's what the `()` is for. No parameter is passed at that point. The function that is actually called up button click is the *returned* function; you could put an `event` parameter there, even though that code doesn't use it. – Pointy Aug 05 '20 at 15:37
  • @Pointy I think that I love you. Thanks very much, it works. – Commander Aug 05 '20 at 15:42

0 Answers0