26

Here is my function atm:

$(document).ready(function() {

$("#knd").click(function() {
    $("#wars").animate({
        opacity:'1'
    }); 


}); 

}); 

It works great but I want to add the option to remove the box with another click, figured I'd do it with an if statement but I'm not 100% on the syntax in Jquery. I want something along the lines of:

if(#wars is visible) {

$("#wars").animate({
         opacity:'0'
    }); 

How would I write the if statement for this?

SOLUTION:

$(document).ready(function() {

$("#knd").click(function() {
if($("#wars").css('opacity') === '0') {         
    $("#wars").animate({
    opacity:'1'
    }); //end of animate
}


else {
$("#wars").animate({
    opacity:'0'
});  //end of animate
}


}); //end of KND click function
}); //end of doc ready function

I had to change the visibility and set it to opacity instead, then it worked like a charm with the if statement.

Duplo W
  • 619
  • 6
  • 11

3 Answers3

5

You can use .is() along with :visible selector:

if($('#knd').is(':visible')) {
    $("#wars").animate({
        opacity:'0'
    }); 
}
Felix
  • 37,892
  • 8
  • 43
  • 55
3

You can also check the following condition :

if($('#knd').css('visibility') === 'visible') {
    $("#wars").animate({
        opacity:'0'
    }); 
}

Suggested solution:

if($('#wars').css('opacity') === '0') {
    $("#wars").animate({
        opacity:'1'
    }); 
}
mohamedrias
  • 18,326
  • 2
  • 38
  • 47
1

You can use .is(':visible') but you can use fadeToggle() or slideToggle().

*Toggle() function automatically does what you're intending to do, with effects.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95