Home >>jQuery Tutorial >jQuery offsetParent() Method

jQuery offsetParent() Method

jQuery offsetParent() Method

jQuery offsetParent() method in jQuery is used to returns the first positioned parent element.

Syntax:
$(selector).offsetParent()
Here is an Example of jQuery offsetParent() Method:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(".btn1").click(function(){
$(".txt1").offsetParent().css("background-color", "#36a7e3");
});
});
</script>
</head>
<body>
<button class="btn1">click me to Set background-color</button>
<div class="box1" style="border:1px solid black;width:70%;position:absolute;left:10px;top:50px">
<div class="box1" style="border:1px solid black;margin:50px;background-color:#34e3d4">
<p class="txt1">click the button to set the background color of the parent div</p>
</div></div>
</body>
</html>

Output:


click the button to set the background color of the parent div