June 30, 2025
Explain jQuery.contains() method
In jQuery contains is one of the import functions. By using it we can check if an HTML element present in another HTML element or not. This function requires two arguments. First argument is container , where we need to check if element exist. Second argument is the HTML argument which needs to be searched. It returned true if the searched element exists in container element otherwise it returns false. Example is mentioned below:
jQuery.contains() method example
<html>
<head>
<title>Example</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div id="container_elem">
<div id="to_search_elem">Hello</div>
</div>
<script>
var container_elem = document.getElementById("container_elem");
var to_search_elem = document.getElementById("to_search_elem");
alert(jQuery.contains(outer, inner))
</script>
</body>
</html>