is() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:is

Description

The is() method checks if one of the selected elements matches the selectorElement.

Syntax

Parameter Require Description
selectorElement Required. a selector expression, element or a jQuery object.
function(index,element) Optional. a function to run for the group of selected elements.
  • index - the index position of the element
  • element - the current element (the "this" selector can also be used)

If the parent of <p> is a <div> element, alert some text:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("p").click(function(){
        if ($("p").parent().is("div")) {
            console.log("Parent of p is div");
        }/*  ww  w  .  j ava 2  s .co  m*/
    });
});
</script>
</head>
<body>

<div>
  <p>click me and check console</p>
</div>

</body>
</html>

Related Tutorials