jQuery: How to test or check whether an element exists

by Yang Yang on April 16, 2009

Share This Article:
Subscribe to Kavoir: blog feed

.length property in JavaScript returns the length or number of elements inside an array or the string length. While jQuery selector mechanism $(“elm”) returns an array of DOM objects (such as elements), you can also get the number of the length of the returned objects array by:

$("#some").length

To check the existence of the element, just check if the returned value of length is zero:

if ($("#some").length) { //#some element exists }

Share This Article:
Subscribe to Kavoir: blog feed

You should also read:

Leave a Comment

Previous post:

Next post: