javascript - Function won't execute - Stack Overflow

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

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 ?

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 ?

Share Improve this question edited Jan 25, 2010 at 8:30 Justin Johnson 31.3k7 gold badges66 silver badges89 bronze badges asked Jan 25, 2010 at 8:02 bob dobbsbob dobbs 1,1532 gold badges7 silver badges5 bronze badges 1
  • I didn't get alert from SO during editing, I roll it back to your edition, jleedev, because yours better. – YOU Commented Jan 25, 2010 at 8:06
Add a ment  | 

4 Answers 4

Reset to default 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)

Change your if statement to:

if (menuVisible == 0)

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

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

You can change:

if (menuVisible = 0)

to:

if (!menuVisible)

to solve this.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744720591a4589884.html

相关推荐

  • javascript - Function won't execute - Stack Overflow

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

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信