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.

p.s. I increased my AdSense revenue by 200% using AI 🤖. Read my Ezoic review to find out how.

Leave a reply

Your email address will not be published.