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 ?
give a positive ratinggive a negative rating
15 Jul 2020 at 04:43 PM

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');" />

give a positive ratinggive a negative rating
31 Aug 2020 at 04:37 PM
Share on FacebookShare on TwitterShare on LinkedInSend email
2023 AnswerTabsTermsContact us
How would you rate your experience with AnswerTabs ?
Very badVery good
Thank you for your feedback.