Zachrip
Zachrip

Reputation: 3382

Javascript adding event listener with custom callback?

So I am making a game and I want everything to be dynamic so I can use it to make other things. I'm trying to set the event listener callback to a custom callback. I'm just testing it with keydown for now, but when I push my keys, nothing outputs in the console:

Here is the registerKeyListener function:

function registerKeyListener(id, type, callback){
    document.getElementById(id).addEventListener(type, callback, false);
}

Here is how I call it:

registerKeyListener("game", "keyDown", move);

Where move is:

function move(){
    console.log("move function called");
}

Upvotes: 2

Views: 4469

Answers (1)

acdcjunior
acdcjunior

Reputation: 135752

Your function works. The event is keydown, not keyDown.

See demo of your code.

Though there can be events with upper case letters, all the usual ones are lower case only. Check here for reference: https://developer.mozilla.org/en-US/docs/Web/Reference/Events

Upvotes: 4

Related Questions