function foo() {
console.log("clicked");
}
element.addEventListener("click", foo());
How come foo()
is invoked automatically when the script loads, but foo
is not? Also, what if I want to pass an argument to the function like this:
function foo(elem) {
console.log(elem, "clicked");
}
element.addEventListener("click", foo("element"));