jQuery - offsetParent( ) Method


Advertisements


Description

The offsetParent() method returns a the positioned parent (e.g. relative, absolute) of the first selected element.

Syntax

Here is the simple syntax to use this method −

selector.offsetParent()

Parameters

Here is the description of all the parameters used by this method −

  • NA

Example

Following is a simple example a simple showing the usage of this method.

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
		
      <script type="text/javascript" language="javascript">
         $(document).ready(function(){
            $("p").offsetParent().addClass('hilight');
         });
      </script>
		
      <style>
         .hilight { background:yellow; }
      </style>

   </head>
	
   <body>
      <scan>Top Element</scan>
		
      <div style="position:relative;">
         <div>sibling<div>child</div></div>
         <p>sibling</p>
         <scan>sibling</scan>
      <div>
		
   </body>
	
</html>

This will produce following result −

<html>
   <head>
      <title>The jQuery Example</title>
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
		
      <script type="text/javascript" language="javascript">
         $(document).ready(function(){
            $("p").offsetParent().addClass('hilight');
         });
      </script>
		
      <style>
         .hilight { background:yellow; }
      </style>

   </head>
		
   <body>
	
      <scan>Top Element</scan>
		
      <div class="hilight" style="position:relative;">
         <div>sibling<div>child</div></div>
         <p>sibling</p>
         <scan>sibling</scan>
      <div>
		
   </body>
	
</html>

This will produce following result −


jquery-traversing.htm

Advertisements