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 1
Div 2
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”.
Click me to check if the paragraph "check-me" exists
This is the paragraph "check-me"
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:
Click me to check if the paragraph "check-me" exists
This is the paragraph "check-me".
<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("Check me too now exists!
").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:
Click me to check if the paragraph "check-me" and "check-me-too" exist
This is the paragraph "check-me"
<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("Check me too now exists!
");
}
});
</script>
Hopefully this article has been useful for you to understand how to use jQuery to check if an element exists.