How to Determine if an Element Exists with jQuery?

jQuery makes it easy to determine if an element in your DOM exists.

if ( $('#elementID').length ) 
{
 // do something
}

Simple enough, right?

Clip to Evernote

2 notes / Permalink

Tags: jquery tips code

How to select children of $(this) in jQuery

It’s simple really…

$("img", this);

The above jQuery translation is just a short version of

$(this).find("img");

So using the find() method would be a touch faster, but both accomplish the same task.

This differs from the standard selection of a child element which is:

$(div > img);

Clip to Evernote

1 note / Permalink

Tags: jquery javascript

How to use jQuery to see if an element exists

To see if an element exists in jQuery you simply need to check for a length

$("#myElement").length

If the above ID has a length the statement returns true.

Tested in jQuery v1.4.2

Clip to Evernote

1 note / Permalink

Tags: jQuery code