Get TOP and LEFT position of image element - Javascript

How can I get the position of image element from the top and left side by Javascript function ? Would you please provide an example that work ?
15 Jul 2020 at 04:43 PM
0give a positive ratinggive a negative ratingshow more

To get the top and left position of image or other HTML element, you can use the solution below. It is based on offsetTop and offsetLeft functions. The position is checked by using ID of element, so with the function you can get the position of multiple elements.

<script type="text/javascript">

function getPosition( elementID ) {

var e = document.getElementById(elementID);
if ( e.offsetParent ) {
var top = e.offsetTop;
var left = e.offsetLeft;
alert('Element position from top is: '+top+'px. Element position from left is:'+left+'px.');



<img src="abc.png" alt="image abc.png" id="photo" style="position: absolute; top: 50%; left: 50%;" onMouseOver="javascript: getPosition('photo');" />

31 Aug 2020 at 04:37 PM
0give a positive ratinggive a negative ratingshow more
Share on FacebookShare on TwitterShare on LinkedInSend email
Follow us on Facebook
2022 AnswerTabsTermsContact us