Home > Tips & Tricks > Check If Element Exists with jQuery

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 😉

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment