Posted By

karlhorky on 07/27/10


Tagged

element check DOM jquery exists defined exist domelement


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

koyotebr


jQuery: Check for existence of element


 / Published in: JavaScript
 

URL: http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_test_whether_an_element_exists.3F

From the jQuery documentation:

You can use the length property of the jQuery collection returned by your selector.

Note: It isn't always necessary to test whether an element exists. The following code would show the item if it exists, and do nothing (no errors) if it did not:

  1. if ( $('#myDiv').length ) {
  2. alert('#myDiv exists!');
  3. }

Report this snippet  

You need to login to post a comment.