offsetParent() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:offsetParent

Description

The offsetParent() method returns the first positioned parent element.

The following code shows how to set the color of the closest positioned parent element of the <p> element:

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(){
    $("button").click(function(){
        $("p").offsetParent().css("color", "red");
    });//from w w  w . j a  v a 2 s  . c  o m
});
</script>
</head>
<body>

<button>Set background-color</button>

<div style="border:1px solid black;width:70%;position:absolute;left:10px;top:50px">
<div style="border:1px solid black;margin:50px;background-color:yellow">

</div></div>

</body>
</html>

Related Tutorials