jQuery position() get element relative to parent

Description

jQuery position() get element relative to parent

View in separate window

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Find Element's Position Relative to the Parent</title>
<style>
     #box{/*  w ww . j a va 2 s  . co  m*/
        width:150px;
        height:100px;
        background: orange;
    }
    .container{
        margin: 20px;
        padding: 30px;
        border: 2px solid #666;
        position: relative;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
    $(document).ready(function() {
        $("button").click(function(){
            var position = $("#box").position();
            document.getElementById("demo").innerHTML = 
               "left: " + position.left + ", top: " + position.top;
        });
    });
</script>
</head>
<body>    
    <p id="demo"></p>
    <div class="container">
        <button type="button">Get Box Position</button>
        <div id="box"></div>
    </div>
</body>
</html>



PreviousNext

Related