$(document).ready(SetupButtonClicks());
function SetupButtonClicks() {
$('#btnJavaPHP').click(DoPHPStuff());
}
function DoPHPStuff() {
//stuff
}
I have this code in my javascript file, when I debug it I see it call SetupButtonClicks()
like it should but after that is done it calls DoPHPStuff()
. DoPHPStuff()
should only be called when btnJavaPHP
is clicked. What am I doing wrong?
Change your SetupButtonClicks function:
$('#btnJavaPHP').click(DoHPStuff);
The way you've got it coded, you're telling Javascript to call the function, not to use it as the "click" handler. The parentheses are an operator that causes a function to be called.
Remove the ()
.
By writing $(document).ready(SetupButtonClicks())
, you are calling SetupButtonClicks
and passing its return value to ready
.
Similarly, by writing $('#btnJavaPHP').click(DoPHPStuff())
, you are calling DoPHPStuff
(immediately) and passing whatever it returns to click()
.
You need to pass the functions themselves by writing $(document).ready(SetupButtonClicks)
and $('#btnJavaPHP').click(DoPHPStuff)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With