How do I test whether an element exists?

January 19, 2010 | Filed Under JQuery | Comments Off

* You can use the length property of the jQuery collection returned by your selector: should i take

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();

Comments

Comments are closed.

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker