Onisiforos
Onisiforos

Reputation: 15

jQuery function it does not get called

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

Answers (4)

Fabian De Leon
Fabian De Leon

Reputation: 126

Your declaration in variable is wrong.

var hello=1;

Upvotes: 0

Tom R.
Tom R.

Reputation: 198

You're using

var hello==1; 

instead of

var hello=1;

Upvotes: 3

hsz
hsz

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

wrivas
wrivas

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

Related Questions