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!
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.
You can use .is(':visible') selects all elements that are visible.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With