Home >>jQuery Tutorial >jQuery position () Method

jQuery position () Method

jQuery position () Method

jQuery position () method is used to returns the position (which is relative to its parent element) of the first matched element and also returns an object with two properties the top and left positions in pixels.

Syntax:

$(selector).position()

Here is an Example of jQuery position () 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(){
var x = $(".txt").position();
alert("Top position: " + x.top + " Left position: " + x.left);
});
});
</script>
</head>
<body>
<p class="txt">PHPTPOINT.</p>
<button class="btn1">click me to return the top and left position</button>
</body>
</html>

Output:

PHPTPOINT.


No Sidebar ads