nn2 nn2 - 3 months ago 9x
HTML Question

Calling addEventListener on a element produce a "Not enough arguments" error

My code works on Chrome but not on Firefox... in Firefox... whenever this piece of code launches..


I get this error:

Not enough arguments
[Break On This Error]


I have
var canvas
near the top as well as this:

canvas = document.getElementById("canvas");
canvas.width = 512;
canvas.height = 352;
context = canvas.getContext("2d");

I do not know why it works fine on Chrome but not Firefox...


You need a function that executes when the event fires. It can be specified either inline like this:

canvas.addEventListener('keydown', function(e) {
    // your code here to handle the event

or it can be specified like this:

function keyHandler(e) {
    // your code here to handle the event

canvas.addEventListener('keydown', keyHandler);

Without an event handler function, there is nothing for the event handler to do as no code would execute when the event fires.