Q:

How to check if an element is hidden using jQuery

0

How to check if an element is hidden using jQuery

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the jQuery :hidden Selector

The jQuery :hidden selector can be used to test whether an element is hidden or not on a page.

You can also use this selector to test the elements whose width and height are set to 0 as well as the form elements with the attribute type="hidden". Let's see how it works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Test If an Element is Hidden</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        // show hide paragraph on button click
        $("p").toggle("slow", function(){
            // check paragraph once toggle effect is completed
            if($("p").is(":hidden")){
                alert("The paragraph  is hidden.");
            } else{
                alert("The paragraph  is visible.");
            }
        });
    });
});
</script>
</head>
<body>
    <button type="button">Toggle Paragraph Display</button>
    <p>Lorem ipsum dolor sit amet adipi elit...</p>
</body> 
</html>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

JavaScript / jQuery Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to select all visible or hidden elements in a ... >>
<< How to Redirect to Another Web Page Using jQuery o...