Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check variable true with if statement

Here is my code:

$(val1).mouseleave(function () {
    flag = false;
    $(val3 + "," + val4).mouseenter(function () {

        flag = true;
        //alert(flag);

    });
    if (flag == true) {
        //alert("if"+flag);
        $(".big" + i + j + "boxer").show();
        $(".big" + i + "box").show();
        $(".big" + i + "box").append('<div class="opacity" style="background-color:rgba(00,00,00,0.77);position:absolute;top:0;right:0;left:0;bottom:0;"></div>')
        $(".small" + i + "box").append('<div class="opacity" style="background-color:rgba(00,00,00,0.77);position:absolute;top:0;right:0;left:0;bottom:0;"></div>')

    } else {
        //alert("else"+flag);
        $('.opacity').remove();
        $(val2).hide();
        $(val3).show();

    }
});

It's not meeting flag = true condition. If I alert within $(val1).mouseleave(function(){});, it shows that flag = true; but when I alert it outside of $(val1).mouseleave(function(){});, it shows flag = false.

Well, let me explain: I have 4 blocks val1,val2,val3 and val4. When the users leave val1 and enter the val3 or val4 block, I want to set/add opacity class ... if they do not enter into val3/val4 but go into val2 or another block block then I want to remove opacity class.

like image 801
Friend Avatar asked Jul 02 '13 11:07

Friend


People also ask

Can you use == with Booleans?

Boolean values are values that evaluate to either true or false , and are represented by the boolean data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".

How do you do a boolean in an if statement?

An if statement checks a boolean value and only executes a block of code if that value is true . To write an if statement, write the keyword if , then inside parentheses () insert a boolean value, and then in curly brackets {} write the code that should only execute when that value is true .

When condition in if statement is true?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Does an if statement return true or false?

The test can be any expression that evaluates to a boolean value – true or false – value (boolean expressions are detailed below). The if-statement evaluates the test and then runs the body code only if the test is true.


1 Answers

At the point where you do the if test:

if(flag==true)

...flag will always be false, because you set it to false just before that. The only place it gets set to true is inside the mouseenter handler that you bind there but that handler function doesn't get called at that point.

Let me add some comments to the beginning of your code to try to make that clearer:

$(val1).mouseleave(function () {
    flag = false;                                  // set flag to false
    $(val3 + "," + val4).mouseenter(function () {  // bind a mouseenter
        flag = true;                               // that won't be called immediately
        //alert(flag);                             // so won't change flag yet
    });
    if (flag == true) {                            // flag is still false

It doesn't make sense to bind a mouseenter handler from inside the mouseleave handler, because that means every time the mouseleave occurs you bind an additional mouseenter handler to the same elements.

I can't really suggest a specific solution because you haven't explained what effect you are trying to achieve. (But I'd probably start by moving that mouseenter code somewhere else.)

like image 144
nnnnnn Avatar answered Sep 18 '22 11:09

nnnnnn