I have a function which expect from parameter JQuery object. I need to find if JQuery object exists in DOM or not (in $('<div></div>')
case).
I check for duplicates and found several answers (link1, link2, link3). Can some expert shine some light witch is the best (fastest) way to do it.
Options are:
$.contains //not working
$a.find
$a.parent()
$a.closes()
document.contains()
$a[0].isConnected
Snippet:
var a = $("#test"); //length 1
var b = $('<div></div>'); //length 1
console.log("contains: ", $.contains(document, a), $.contains(document, b)); //false, false
console.log("find: ", !!$(document).find(a).length, !!$(document).find(b).length); // true, false
console.log("parent: ", !!a.parent().length, !!b.parent().length); // true, false
console.log("closest: ", !!a.closest('body').length, !!b.closest('body').length); //true, false
console.log("document.contains: ", document.contains(a[0]), document.contains(b[0])); //true, false
console.log("isConnected: ", a[0].isConnected, b[0].isConnected); //true, false
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="test"></div>
</body>
</html>
Thank you.