jQuery: if element exists

Last updated on

Use the .length property of the jQuery collection returned by your selector. To check if a div element with the ID of myDiv exists:

if ( $( "#myDiv" ).length ) {
    alert('It exists!');

Let me know if this helped. Follow me on Twitter, Facebook and YouTube, or 🍊 buy me a smoothie.

Leave a reply

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