Checking for the Existance of an Element using jQuery

Written by James Mansson on February 22, 2014 Categories: jQuery

There is a simple way to determine whether an element exists on a web page using jQuery. This involves using the length property of the object returned by a jQuery selector. For instance, if you need to check whether there is an element with the ID of size before performing some operation, you can use the following jQuery:

if ($("#size").length > 0)
{
  /* Do whatever you need to do */
}

The selector will always return an object that encapsulates whatever matches the selector. This can be any number of elements, including zero. The length property is used to indicate the number of elements selected.

No Comments on Checking for the Existance of an Element using jQuery

Leave a Reply

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