Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery if statement to check visibility

I'm trying to write a script that will hidden/show div depending on other elements visibility. The action should take place when i click on other element. Here's what I've wrote so far:

$('#column-left form').hide();     $('.show-search').click(function() {         $('#column-left form').stop(true, true).slideToggle(300);         if( $('#column-left form').css('display') == 'none' ) {             $("#offers").show();         } else {             $('#offers').hide();         }     }); 

It hides the div, but it doesn't come back when I hide the form. Will be greatful for any help :)

edit:

Ok, I've managed to achive the desired effect by writing this:

$('#column-left form').hide();     $('.show-search').click(function() {         if ($('#column-left form').is(":hidden")) {             $('#column-left form').slideToggle(300);             $('#offers').hide();         } else {             $('#column-left form').slideToggle(300);             $("#offers").show();         }     });    

I don't know if it's written correctly but it works ;) Thanks everybody for help!

like image 438
Tomarz Avatar asked Dec 23 '11 12:12

Tomarz


People also ask

How check element is hidden or not in jQuery?

To check if an element is hidden or not, jQuery :hidden selector can be used. .toggle() function is used to toggle the visibility of an element.

Is Div visible jQuery?

You can use .is(':visible') selects all elements that are visible.

How can I tell if a div is in a viewport?

Summary. Use the getBoundingClientRect() method to get the size of the element and its relative position to the viewport. Compare the position of the element with the viewport height and width to check if the element is visible in the viewport or not.


1 Answers

You can use .is(':visible') to test if something is visible and .is(':hidden') to test for the opposite:

$('#offers').toggle(!$('#column-left form').is(':visible')); // or: $('#offers').toggle($('#column-left form').is(':hidden')); 

Reference:

  • http://api.jquery.com/is/
  • http://api.jquery.com/visible-selector/
  • http://api.jquery.com/hidden-selector/
like image 178
ThiefMaster Avatar answered Sep 18 '22 19:09

ThiefMaster