If the parent of <p> is a <div> element, alert some text:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("p").click(function(){ if ($("p").parent().is("div")) { document.getElementById("demo").innerHTML = "Parent of p is div"; }/* ww w . ja v a 2 s. c o m*/ }); }); </script> </head> <body> <p id="demo"></p> <div> <p>Click me to find out if I my parent is a div element.</p> </div> </body> </html>
The is()
method checks if one of the selected elements matches the selector_Element.
$(selector).is(selector_Element,function(index,element))
Parameter | Optional | Description |
---|---|---|
selector_Element | Required. | a selector expression, element or a jQuery object to match. Returns true if there is at least one match Returns false if not. |
function(index,element) | Optional. | a function to run for the selected elements. index - the index position of the element element - the current element |