Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if HTML element is/is not hidden? [duplicate]

Tags:

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> 
like image 857
PRO_gramista Avatar asked Mar 24 '14 13:03

PRO_gramista


People also ask

How do you check if an HTML element is hidden?

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.

How do you know if an element is visible on screen?

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.

How do I check if a div is visible?

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.


1 Answers

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

like image 88
Oli M Avatar answered Sep 28 '22 10:09

Oli M