jquery check if element exists

Today, We want to share with you jquery check if element exists.In this post we will show you javascript check if id exists, hear for javascript check if element exists we will give you demo and example for implement.In this post, we will learn about javascript Check If Element Exists Or Not with an example.

How to check if an element exists in jQuery

$('elemId').length doesn't work for me.

Example : 1

if($('#firstarea').length){
    alert("firstarea exists");
}else{
    alert("firstarea does not exists");
}

Example : 2 jQuery length example

<html>
<head>

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>

</head>

<body>

<h3>jQuery check if an element exists</h3>

<script type="text/javascript">

  $(document).ready(function(){

    $("#buttonDiv1").click(function () {
        
    if($('#firstarea').length){
        alert("firstarea exists");
    }else{
        alert("firstarea does not exists");
    }
            
    });

    $("#buttonDiv2").click(function () {
        
    if($('#secondarea').length){
        alert("secondarea exists");
    }else{
        alert("secondarea does not exists");
    }
            
    });
    
  });
</script>
</head><body>

<div id="firstarea">
    <b>This is DIV element which has an ide of "firstarea"</b>
</div>

<br/>
<br/>
<br/>

<input type='button' value='firstarea exists?' id='buttonDiv1'>
<input type='button' value='secondarea exists?' id='buttonDiv2'>

</body>
</html>

Example : 3

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Test If an Element Exists - www.pakainfo.com</title>
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        if($("#levelCart").length){
            alert("The element you're testing is present.");
        }
    });
});
</script>
</head>
<body>
    <div id="levelCart"></div>    
    <p>Hello Guyes Welcome to our online web tutorials example</p>
    <button>Check Element</button>
</body>
</html>

I hope you get an idea about jquery check if element is visible.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.