How do I test whether an element exists?

How do I test whether an element exists?

Postby tomrff » Thu Apr 28, 2011 1:51 pm

you can use the length property of the jQuery collection returned by your selector:

if ( $(‘#myDiv’).length )
$(‘#myDiv’).show();

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:

$(‘#myDiv’).show();
tomrff
 
Posts: 33
Joined: Sat Jan 08, 2011 10:12 pm

Return to JQuery

Who is online

Users browsing this forum: No registered users and 1 guest

cron