Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write this if condition with Jquery? [duplicate]

Tags:

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.

like image 567
Duplo W Avatar asked May 08 '14 16:05

Duplo W


People also ask

What is noConflict in jQuery?

The noConflict() method releases jQuery's control of the $ variable. This method can also be used to specify a new custom name for the jQuery variable. Tip: This method is useful when other JavaScript libraries use the $ for their functions.

How to check if a value exists in an array in jQuery?

1) Using jQuery If you are someone strongly committed to using the jQuery library, you can use the . inArray( ) method. If the function finds the value, it returns the index position of the value and -1 if it doesn't.

What do === mean in jQuery?

=== This is the strict equal operator and only returns a Boolean true if both the operands are equal and of the same type.


1 Answers

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

if($('#knd').is(':visible')) {     $("#wars").animate({         opacity:'0'     });  } 
like image 191
Felix Avatar answered Nov 15 '22 18:11

Felix