4
$('#buttons1').on('click', function(event) {             

    $('#button1content').toggle('show');
    var wasVisible = $("#button1content").is(":visible");
    if(!wasVisible) {
        $("#buttons1").css("opacity", "0.5");
    }
});

Toggle works perfectly fine, but whatever is inside the if statement doesn't get executed when #button1content is no longer visible. boo. It could be another part of my code that is messing it up, but I only want to know if there is anything wrong with this.

3
  • 1
    can you provide plunkr or jsfiddle ? Commented Aug 19, 2015 at 2:39
  • Typo? Did you mean to pass 'slow' vs. 'show'? If that's not meant to be a duration, .toggle() can be called without arguments to change whether the element is showing or hiding. Commented Aug 19, 2015 at 3:12
  • thanks for helping everyone! Commented Aug 19, 2015 at 8:31

2 Answers 2

2

your variable wasVisible will always return a true

  • you can place the toggle on the last part.

you can reorder your code like this.

$('#buttons1').on('click', function(event) {             

    var wasVisible = $("#button1content").is(":visible");
    if(!wasVisible) {
        $("#buttons1").css("opacity", "0.5");
    }
    $('#button1content').toggle('show'); 
});

JSFIDDLE DEMO


OR


  • just simply remove the 'show' on the toggle just use

    $('#button1content').toggle();

like this:

$('#buttons1').on('click', function(event) {             

    $('#button1content').toggle();
    var wasVisible = $("#button1content").is(":visible");
    if(!wasVisible) {
        $("#buttons1").css("opacity", "0.5");
    }
});

JSFIDDLE DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

The if statement is not reachable because you always showed button1content

 $('#button1content').toggle('show');

So var wasVisible = $("#button1content").is(":visible"); will always result to true

7 Comments

if statement doesn't get executed when #button1content is no longer visible ?
how toggle always show ?? what's the purpose of using toggle ?
everytime button is clicked, button1content is always visible,
then? tell me whats your opinion
@Euphoria, you did spot the problem but your answer could be modified to say that, the if statement always results to be falsey because wasVisible is always true. If statement is not reachable is misleading :)
|

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.