How to check if an element exists in jQuery

  1451 views   2 years ago jQuery

Use the jQuery .length Property

You can use the jQuery .length property to determine whether an element exists or not in case if you want to fire some event only if a particular element exists in DOM.

Here's an example that displays an alert on button click if the specified element exists.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>jQuery Test If an Element Exists</title>
<script src=""></script>
            alert("The element you're testing is present.");
    <div id="myDiv"></div>    
    <p>The quick brown fox jumps over the lazy dog</p>
    <button>Check Element</button>


Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]