0

I've got a simple question, when adding an event listener in Javascript, can you call an existing declared function using its name (without storing it in a variable)? I know defining a new function within the event listener arguments (Ex. #1) works, as well as inserting a declared function into a variable and using it (Ex. #2), but when I'm trying to call a declared function without a variable by using its name it just executes the function once (Ex. #3)

var my_button = document.querySelector("my_button")

/* Ex. 1 - works */
my_button.addEventListener("click", function() {
console.log("Click Occured");
});

/* Ex. 2 - works */
var func_a = function My_Click()
{
  console.log("Click Occured");
}

my_button.addEventListener("click", func_a);

/* Ex. 3 - won't work */
function func_a()
{
  console.log("Click Occured");
}

my_button.addEventListener("click", func_a())

I'd also really appreciate an explanation about why it works that way, thanks a lot in advance!

rzk666
  • 163
  • 11

0 Answers0