Categories
JavaScript Tips & Tutorials jQuery Tips & FAQ

jQuery: How to test or check whether an element exists

.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 }

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.