Skip to content

Commit 1b0d12b

Browse files
committed
Explain how to test if element attached to main DOM
1 parent bb6f577 commit 1b0d12b

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

page/using-jquery-core/faq/how-do-i-test-whether-an-element-exists.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,14 @@ Note that it isn't always necessary to test whether an element exists. The follo
1717
```
1818
$( "#myDiv" ).show();
1919
```
20+
21+
If you need to understand if element is present in main DOM tree, or if you're previously stored selector set in variable, use [jQuery.contains](https://api.jquery.com/jQuery.contains/)
22+
23+
```
24+
var elements = $('#myDiv');
25+
if ($.contains(document, elements)) {
26+
elements.show();
27+
}
28+
```
29+
30+
Because modern browsers detaching elements in [DocumentFragment](https://developer.mozilla.org/ru/docs/Web/API/Document/createDocumentFragment), `.length` will always return constant value, even if element already removed from page.

0 commit comments

Comments
 (0)