Skip to content

Latest commit

 

History

History
19 lines (13 loc) · 457 Bytes

how-do-i-test-whether-an-element-exists.md

File metadata and controls

19 lines (13 loc) · 457 Bytes
<script>{ "title": "How do I test whether an element exists?" }</script>

Use the .length property of the jQuery collection returned by your selector:

if ( $( "#myDiv" ).length ) {

	$( "#myDiv" ).show();

}

Note that it isn't always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no errors) if it does not:

$( "#myDiv" ).show();