Attribute onclick=”function()” not functioning as intended?

One of the many reasons not to use onxyz="..."-style event handlers is that any functions you call from them must be globals. Your functions aren’t, they’re local to the window.onload callback.

Instead, just assign the function reference directly, either somewhat old-fashioned:

left.onclick = playerLeft;

or using more modern techniques that allow for multiple handlers:

left.addEventListener("click", playerLeft);

Leave a Comment