3

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 ?

Justin Johnson
31.3k7 gold badges67 silver badges89 bronze badges
asked Jan 25, 2010 at 8:02
1
  • I didn't get alert from SO during editing, I roll it back to your edition, jleedev, because yours better. Commented Jan 25, 2010 at 8:06

4 Answers 4

8

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)
Justin Johnson
31.3k7 gold badges67 silver badges89 bronze badges
answered Jan 25, 2010 at 8:04
Sign up to request clarification or add additional context in comments.

Comments

4

Change your if statement to:

if (menuVisible == 0)

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

answered Jan 25, 2010 at 8:04

Comments

2

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 :)

answered Jan 25, 2010 at 8:26

Comments

2

You can change:

if (menuVisible = 0)

to:

if (!menuVisible)

to solve this.

answered Jan 25, 2010 at 8:09

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.