Dave's Brain

Browse - Programming Tips - How can I check if something exists using jQuery?

Date: 2013sep20
Library: jQuery
Language: javaScript

Q.  How can I check if something exists using jQuery?

A.  Use size() like this:

if ($('#mything').size() == 0) { // does not exist } else { // is there }
Here we are just checking for one id but it works for any selector. For example to see if the document has any images:
if ($('img').size() == 0) { alert('No images'); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.