Reputation: 15
Hello everyone hope you are well.
I am trying to call a function in my jQuery file and for some reason it doesn't get called if I call it from an if statement. However the same function gets called for when I call it using .click().
The if statement works fine.
Here is my jQuery code.
$(document).ready(function () {
var hello = 1;
if (hello == 1) {
console.log("True");
helloWorld();
} else {
console.log("False");
}
$('#en').click(helloWorld(pathname));
function helloWorld() {
return function () {
console.log("function called");
}
});
});
Upvotes: 0
Views: 92
Reputation: 152216
When assigning hello
variable, you test it if it is equal to 1
, not assigning 1
to variable. Just try with:
var hello = 1;
Upvotes: 1
Reputation: 509
Your function return another function, if you want to call it, this is the correct way:
helloWorld()();
Or
var myFunc = helloWorld();
myFunc();
Upvotes: 0