$(document).ready(function() {
$("button").on("click", TestFunction());
});
function TestFunction() {
console.log("Function called");
$b = $("#test");
$b.remove();
}
vs
$(document).ready(function() {
$("button").on("click", function() {
$b = $("#test");
$b.remove();
});
});
The first one removes the button #test as soon as the page loads. No need to click the button, it just removes it. The second works as expected. The button is displayed and is not removed until it is called.
What part of the Javascript syntax / jQuery API am I not undertanding? I thought the handler parameter passed to .on() was a function. I passed it a function in the first example, and it seems to have called it, but it ignores the "click".
Shouldn't whatever is it inside a function be ignored until the function is called?