1

So I'm learning Javascript and I have a doubt on changing a global variable with boolean variable, while changing the attr of visibility on an element.

The code is this:

var lastView=false;

$("#idShipmentActionsCombo-icon").on('click', function(){

if (lastview=false){

$('#idShipmentActionsCombo-lb').attr('style', 'visibility: visible');
    lastView=true;

}

else if(lastView=true){

$('#idShipmentActionsCombo-lb').attr('style', 'visibility: hidden');
    lastView===false;

    }
}

So #idShipmentActionsCombo-icon is the element I click in, #idShipmentActionsCombo-lb and this is what I want to show and hide depending on the value of lastView.

Thanks in advance, and I apologize for my English since it's not my main language.

3 Answers 3

1

Since you use jQuery use .toggle() method instead of booleans, conditions and style.

$("#idShipmentActionsCombo-icon").on('click', function(){ 
   $('#idShipmentActionsCombo-lb').toggle();
})
Sign up to request clarification or add additional context in comments.

Comments

0

Looks like you're missing a closing ); at the very end from your .on( In addition, there are a few cases where "===" and "=" are confused and where capitalization is incorrect. See this: http://jsfiddle.net/215sxj90/3/

Comments

0

In my opinion you're confusing assignment with logical operators. The following is the assignment:

lastView = true;

and the following is the logical operator - comparison:

lastView === true

The latter should be used in your conditional statements - if, else if etc.:

var lastView = false;

$("#idShipmentActionsCombo-icon").on('click', function () {

    if (lastview === false) {

        $('#idShipmentActionsCombo-lb').attr('style', 'visibility: visible');
        lastView = true;

    }
    else if (lastView === true) {
        $('#idShipmentActionsCombo-lb').attr('style', 'visibility: hidden');
        lastView = false;

    }
}

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.