jQuery parent Method


jQuery parent Method – jQuery “parent()” is used to get parent of the selected element.

This method becomes useful when you want to get parent of the element in DOM. We are going to explain this method will full example and demo.


jQuery parent() method

Here is syntax for parentsUntil Method –

$(selector).parent(filter);

filter : It is optional, A selector expression used to match(parent) the elements against.

jQuery parent() Method Example with Syntax

Here is an example with demo try it to test the functionality of the parent method-

jQuery parent Method – Example 1

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#parentExample" ).on( "click", function(event) {             
                 $("p").parent().css("background","yellow");
              });
               
          });
</script>

Try it »

jQuery parent() Method Example with Syntax

jQuery parent Method – Example 2

<script type="text/javascript">
         $(document).ready(function(){ 
              $( "#parentExample" ).on( "click", function(event) {             
                 $("p").parent(".myPara").css("background","yellow");
              });
               
          });
</script>

Try it »

The above example will produce the following output as below –

jQuery parent Method Example And Demo


Advertisements

Add Comment

📖 Read More