bob dobbs
bob dobbs

Reputation: 1153

Function won't execute

I'm trying to create a function that will execute on a click, on the condition that a variable is set to 0;

However, the function will not execute, even if the variable is set to 0. (I am using jquery)

var menuVisible = 0 ;
$('#links').click(function(){ 
    if (menuVisible = 0)
    {
        $('#subMenu').show("slow") ;
        menuVisible = 1 ;
    }
});

I'm testing the value of the variable 'MenuVisible' with alert, and it is indeed '0'. So, why won't the function execute ?

Upvotes: 3

Views: 282

Answers (4)

cletus
cletus

Reputation: 625077

You're assigning a value with:

if (menuVisible = 0)

so that will always be false (because it's 0). Change it to:

if (menuVisible == 0)

Upvotes: 8

Hypnus
Hypnus

Reputation: 242

This is just a suggestion, the solutions offered should have worked. If indeed you wanted to create a "on click" open/close situation (that is my assumption from your code so don't mind it if it is wrong ) then instead of that variable and hide()/show() you should use the toggle() function. Have fun :)

Upvotes: 2

lillq
lillq

Reputation: 15389

You can change:

if (menuVisible = 0)

to:

if (!menuVisible)

to solve this.

Upvotes: 2

Daniel Vassallo
Daniel Vassallo

Reputation: 344301

Change your if statement to:

if (menuVisible == 0)

The double equals is the equality operator. Single equals is assignment.

Upvotes: 4

Related Questions