• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / How to Check if Element Exists Using jQuery

How to Check if Element Exists Using jQuery

November 30, 2021 Leave a Comment

To check if an element exists using jQuery, the simplest way is to use the length property.

if($("#div").length) { // if length is greater than 0 then the element exists

Let’s say I have the following HTML:

<div>
<div id="div">Div 1</div>
<div>Div 2</div>
</div>

To check if the div with id “div” exists, we can use the jQuery length method to do this with the following Javascript code:

if($("#div").length) {
   // do whatever you want here
}

If $(“#div”).length is 0, then #div does not exist – and we shouldn’t try to do anything with it. If $(“#div”).length is not 0, then #div exists and we can perform other operations on the element.

If you are using WordPress, don’t forget to change the $ to jQuery as below:

if(jQuery("#div").length) {
   // do whatever you want here
}

Checking the Existence of an HTML Element Using jQuery With a Click

Many times when creating a web page and the user experience, we want to check the existence of various html elements based on an interaction with another element on the web page.

We can check using jQuery very easily by combining the length property with a click event.

Let’s say we have the following HTML code and we want to check to see if there is a paragraph with id “check-me”.

<div id="div">
<p id="click-me">Click me to check if the paragraph "check-me" exists</p>
<p id="check-me">This is the paragraph "check-me"</p>
</div>

We can utilize both the jQuery click() method

and jQuery length property to check if the paragraph exists.

Below is the Javascript code which will allow the user to be check if the element exists:

$("#click-me").click(function(){
    if($("#check-me").length) {
        // do whatever you want here
    }
}); 

The final code and output for this example of how to check if an element exists on click using jQuery and Javascript is below. In the example below, after clicking, you will see that the span updates because “check-me” exists.

Code Output:

Click me to check if the paragraphs “check-me” exists

This is the paragraph “check-me”.

Full Code:


<div class="html-code-output">
<div id="div">
<p id="click-me">Click me to check if the paragraph "check-me" exists</p>
<p id="check-me">This is the paragraph "check-me". <span id="checked-status"></span></p>
</div>
</div>

<script>

$("#click-me").click(function(){
    if($("#check-me").length) {
        $("#checked-status").text("check-me exists!");
    }
});

</script>

How to Check if An Element Doesn’t Exist with jQuery

Many times when designing a web page, we also need to add elements which don’t exist. If an element doesn’t exist, then we need to add it to the DOM before trying to do anything with it.

Let’s take the same example from above and add a little more to it.

First, if “check-me” exists, let’s change the background color of the parent div.

Let’s also check if an element with id “check-me-too” exists. If “check-me-too” doesn’t exist, let’s add the element after the last “check-me” paragraph.

To check if an element doesn’t exist, we can check if the jQuery length property is equal to 0.

if($("#check-me-too").length === 0) { 
// if length is equal to 0 then the element does not exist
}

The resulting Javascript code to do both checks would be:

$("#click-me").click(function(){
    if($("#check-me").length) {
        $("#div").css("background-color","green") // change background color
    }
    if($(#check-me-too").length === 0) {  // if this does NOT exist 
        $("p").add("<p>Check me too now exists!</p>").insertAfter("#check-me");
    }
});

The final code and output for this example of how to check if an element doesn’t exist on click using jQuery and Javascript is below:

Code Output:

Click me to check if the paragraphs “check-me” and “check-me-too” exist

This is the paragraph “check-me”

Full Code:


<div class="html-code-output">
<div id="div">
<p id="click-me">Click me to check if the paragraph "check-me" and "check-me-too" exist</p>
<p id="check-me">This is the paragraph "check-me"</p>
</div>
</div>

<script>

$("#click-me").click(function(){
    if($("#check-me").length) {
        $("#div").css("background-color","green") // change background color
    }
    if($("#check-me-too").length === 0) {  // if this does NOT exist 
        $("#check-me").append("<p>Check me too now exists!</p>");
    }
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to check if an element exists.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Get the Next Sibling of an Element
  • 2.  jQuery focusout – How to Use the focusout() Method on an HTML Form
  • 3.  Using jQuery to Check if Element Has Class
  • 4.  Using jQuery prepend to Insert Element As First Child
  • 5.  jQuery hover – An Interactive Example of the hover Method
  • 6.  Using jQuery to Set a Radio Button to Checked
  • 7.  Using jQuery to Disable Input
  • 8.  jQuery last() – Get the Last Element
  • 9.  Using jQuery to Add a Sibling to an Element
  • 10.  jQuery Display None

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy