-1

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");
        }
    });
});
1
  • 1
    var hello==1 ? this is a condition should not be var hello = 1? Commented Mar 18, 2014 at 12:05

4 Answers 4

3

You're using

var hello==1; 

instead of

var hello=1;
Sign up to request clarification or add additional context in comments.

Comments

1

When assigning hello variable, you test it if it is equal to 1, not assigning 1 to variable. Just try with:

var hello = 1;

Comments

0

Your function return another function, if you want to call it, this is the correct way:

helloWorld()();

Or

var myFunc = helloWorld();
myFunc();

Comments

0

Your declaration in variable is wrong.

var hello=1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.