Q: addEventListener "activating" automatically Basic question I know, but I'm having trouble understanding something. I have a function that simulates a dice throw, with the number of faces as the parameter :
function throw(faces){
//My function here
}

I then have buttons to simulate those launches with different numbers, and I want them to launch throw when they are clicked :
document.getElementById("4").addEventListener("click",throw(4));
document.getElementById("6").addEventListener("click",throw(6));

Etc.
But when I launch the CodePen (here), the functions launch automatically (with the correct parameters though) and then I can't click the buttons. Well I can, but nothing happens.
What am I doing wrong? I have a feeling it's a very basic stuff, but I can't seem to get it. I've seen that if you put throw instead of throw() the function doesn't launch automatically, but how am I supposed to change the parameters then?

A: throw(4) executes/calls the method automatically because you are invoking the function/method using (). If you want the event listener on the button to call throw(4) when clicked, then use the following:
document.getElementById("4").addEventListener("click", function(){
   throw(4);
});

or
document.getElementById("4").addEventListener("click", throw);
function throw (){
  console.log(event.target.id); //event.target.id will give the id of the element clicked
  // your rest of the code here
}


A: Your throw function needs to return a function to the addEventListener. For example:
function throw ( faces ) {
    return function () {
        return Math.floor( Math.random() * faces );
    }
}


A: You can use bind to return a new function with a preset this keyword and arguments (null in this case):
function throw(faces){
  // My function here
}

document.getElementById("4").addEventListener("click", throw.bind(null, 4));
document.getElementById("6").addEventListener("click", throw.bind(null, 6));

Or define a new function inside which executes throw:
document.getElementById("4").addEventListener("click", function() {
    throw(4);
});
document.getElementById("6").addEventListener("click", function() {
    throw(6);
});

