{"id":128,"date":"2019-09-16T16:42:43","date_gmt":"2019-09-16T16:42:43","guid":{"rendered":"https:\/\/tutorialio.com\/?p=128"},"modified":"2025-11-04T10:31:25","modified_gmt":"2025-11-04T10:31:25","slug":"check-if-an-element-is-hidden-or-visible","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/check-if-an-element-is-hidden-or-visible\/","title":{"rendered":"How Can I Check if an Element is Hidden or Visible with JavaScript?"},"content":{"rendered":"<p>Using JavaScript or jQuery to check if an element is hidden or not sounds very simple. However, there are lot of things that you need can determine the final outcome of such a check. For instance, everyone will have a doing criteria for determine if an element is hidden or visible.<\/p>\n<p>In some projects, an element could be considered hidden only if its <code>display<\/code> property is set to <code>none<\/code>.  In other projects, the element would be considered hidden if its opacity is set to 0 or its visible property is set to <code>hidden<\/code>. In this post, we will discuss how to determine the visibility of an element based on all these properties.<\/p>\n<h2 id=\"check-if-an-element-is-visible-or-hidden-using-jquery\">Check if an Element is Visible or Hidden Using jQuery<\/h2>\n<p>There is a <code>:visible<\/code> selector in jQuery which can be used to determine if an element is hidden or visible. This selector considered an element to be hidden if its <code>display<\/code> property is set to <code>none<\/code>, its width and height are explicitly set to 0 or if an ancestor element is hidden so that the considered element is not visible on the page. Form elements <code>type<\/code> attribute set to <code>hidden<\/code> are also considered to be hidden.<\/p>\n<p>You should remember that elements with their <code>opacity<\/code> set to 0 and the <code>visibility<\/code> property set to <code>hidden<\/code> will also be considered visible by this selector. Basically, any element which affects the layout of the webpage is considered visible by this selector.<\/p>\n<p>The selector is not part of the CSS specification which means that it cannot take advantage of the performance boost provided by the inbuilt <code>querySelectorAll()<\/code> method.<\/p>\n<p>Depending on your criteria of visibility, using <code>:visible<\/code> can be very helpful or overkill in some cases.<\/p>\n<h2 id=\"checking-the-display-property-to-determine-if-an-element-is-visibile-or-hidden\">Checking the Display Property to Determine if an Element is Visibile or Hidden<\/h2>\n<p>Another technique to check if an element is visible or hidden is to compare the value of <code>display<\/code> property to <code>none<\/code>. If an element was hidden using the <code>display<\/code> property, this check would return <code>true<\/code>. Checking the value of <code>display<\/code> property is much more faster than using the <code>:visible<\/code> selector.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">if ( $(\"selector\").css('display') == 'none' ){\n    \/\/ Element is hidden\n}<\/code><\/pre>\n<p>One major drawback of using this technique is that an element might be hidden because of its parent but this comparison will still return <code>false<\/code>. Also, it does not take the value of <code>visibility<\/code> or <code>opacity<\/code> property in consideration.<\/p>\n<h2 id=\"using-a-class-to-determine-if-an-element-is-hidden\">Using a Class to Determine if an Element is Hidden<\/h2>\n<p>You can assign classes to different elements and apply CSS properties inside those classes to hide an element. After that, all you have to do is check if that class has been applied to a particular element. The following example illustrates how it works:<\/p>\n<p class=\"lang-name\">CSS<\/p>\n<pre><code class=\"language-css\">.no-display {\n  display: none !important;\n}\n\n.no-visibility {\n  visibility: hidden !important;\n}\n\n.no-opacity {\n  opacity: 0 !important;\n}<\/code><\/pre>\n<p>Now you can show or hide any element by adding or removing these classes using <code>.addClass(class-name)<\/code> and <code>.removeClass(class-name)<\/code> respectively. To check if an element id visible or hidden, you can simply use <code>.hasClass(class-name)<\/code>.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">$(\"selector\").addClass(\"no-display\");\n$(\"selector\").removeClass(\"no-display\");\n\nif($(\"selector\").hasClass(\"no-display\")) {\n  \/\/ Element is Hidden\n}<\/code><\/pre>\n<p>Some elements might be hidden because of their parents, in such cases you can use the <code>.closest(selector)<\/code> method to check if any of the ancestors of our element have these classes applied to them. Using the <code>length<\/code> property on the result of <code>.closest()<\/code> and checking if it is not equal to zero can tell you if an element is hidden because of its ancestors.<\/p>\n<p class=\"lang-name\">JavaScript<\/p>\n<pre><code class=\"language-js\">var elem = $(\"selector\");\n\nif(elem.closest(\"no-display\").length &gt; 0) {\n  \/\/ Element in Hidden Because of a Parent\n}<\/code><\/pre>\n<h2 id=\"quick-summary\">Quick Summary<\/h2>\n<p>Let\u2019s recap everything that we have covered in this tutorial.<\/p>\n<ol id=\"summary-points\">\n<li>You can use the <code>:visible<\/code> selector in jQuery to check if an element is hidden or visible. This selector considers every element which does not affect the layout of the webpage to be hidden. It also take into account the visibility of ancestor element before determining if the current element is visible.<\/li>\n<li>To know if an element has been hidden using the <code>display<\/code> property, you can simply compare the value of display to <code>none<\/code>. Using this technique, you will not be able to determine if an element was hidden because of some property applied on its parent.<\/li>\n<li>You can also use classes to determine if an element is hidden or visible. All you have to do is add a class to element to hide it and then remove the class to show it. If you want to check whether an element is hidden, you will just have to look for the right class in it. You will then also be able to use the jQuery <code>.closest()<\/code> method to determine if an element is not visible because of its parent.<\/li>\n<\/ol>\n<p>Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to check if an element is visible or hidden using jQuery or JavaScript.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using JavaScript or jQuery to check if an element is hidden or not sounds very simple. However, there are lot of things that you need can determine the final outcome of such a check. For instance, everyone will have a doing criteria for determine if an element is hidden or visible. In some projects, an [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[18,19],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-how-to-guides","tag-dom","tag-javascript"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":1,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":10238,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/128\/revisions\/10238"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}