I have my two elements(setProject and setHdc). When clicked, they show other table elements. But I want to make only one group of table elements appear at the same time. for example when the user clicked on "setProject", the "setHdc" element must be hidden. and the same otherwise. Is there any way i can do it as If statment? Or is there a simpler way to do it?
<script> $(document).ready(function(){ $("#setProject ").click(function(){ $("#test1").fadeToggle("fast"); $("#projectTable1").fadeToggle("fast"); $("#projectTable2").fadeToggle("fast"); $("#projectTable3").fadeToggle("fast"); }); }); $(document).ready(function(){ $("#setHdc").click(function(){ $("#hdcTable1").fadeToggle("fast"); $("#hdcTable2").fadeToggle("fast"); }); }); </script>
Another way to check if an element is hidden is to use the window. getComputedStyle() method. It will return the display property value of the object. We can check if the string is equal to none meaning the element is hidden and do something.
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 the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.
You should use
if($(this).is(':visible')){ doSomething(); }else{ doSomethingElse(); }
the else
part will only work for elements with display:none
. elements that have visibility:hidden/opacity:0
will be considered visible
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