Check If Element Exists with jQuery

In this trick, I will show you how you can check if the specific element exists in the document using the jQuery code.

Element exists means when we want to make any condition based on a specific HTML element and firstly we want to check that it exists or not and then trigger the action.

So, to check the HTML element exists, you just have to target that element and get its length of it using the jQuery methods. If the length will be greater than zero, it means the element exists.

In the above code, we checked if the element of the ‘warning‘ class selector exists or not, using the length property of jQuery.

Being Tricky 😉

Leave a Comment