Check if Element Exists With jQuery

To see if an element exists with jQuery you can use the length property .length. This will return the size of the object.


if ( $('#theElement').length > 0 ) {
    // it exists, do something here
}

If your element is present the above code will return true and the code in the if statement will be executed.

This post currently has no responses. What do you think?

You can use basic HTML when posting code, please turn all < characters into &lt; or > into &gt;
If the code is multi-line, use <pre><code></code></pre>

Leave a Reply

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

Current ye@r *