Q:

How to check an element is visible or not using jQuery

0

How to check an element is visible or not using jQuery

All Answers

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

Use the jQuery :visible Selector

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.

Let's check out an example to understand how this selector basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Test If an Element is Visible</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(":visible")){
                alert("The paragraph  is visible.");
            } else{
                alert("The paragraph  is hidden.");
            }
        });
    });
});
</script>
</head>
<body>
    <button type="button">Toggle Paragraph Display</button>
    <p style="display: none;">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 populate state dropdown based on option sel... >>
<< How to get the text inside an element using jQuery...