jQuery-Check if an element is hidden or Not?
----------------------------------------------
:visible and :hidden selector can be used to determine if element is hidden or visible.
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 in open.
These selector return true or false based on the used because jQuery is() to check the selected element with another element, selector or any jQuery object. This method traverses along the DOM elements to find a match, which
satisfies the passed parameter. It will return true if there is a match otherwise returns false.
- <!DOCTYPE html>
- <html>
- <head>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
- </head>
- <body>
- <p>Para Visiable</p>
- <script>
- $(document).ready(function(){
- // show hide paragraph on button click
- if($("p").is(":visible")){
- alert("The paragraph is visible.");
- }
- if($("div").is(":hidden")){
- alert("Div is hidden.");
- }
- });
- </script>
- </body>
- </html>
Elements that are not in a document are considered hidden; jQuery does not have a way to know if they will be visible when appended to a document since it depends on the applicable styles.
Categories: Java Script Tags: #JQuery, #JavaScript,