• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / JavaScript offsetTop – Get the Top Position of Element

JavaScript offsetTop – Get the Top Position of Element

April 3, 2022 Leave a Comment

We can use JavaScript and the offsetTop property of an element to get the top position of that element.

document.getElementById("div1").offsetTop;

The offsetTop property will return the top position of the element relative to the page.

Note that we can also get the top position of an element using jQuery using the offset() method.

Using JavaScript and the offsetTop property to Get the Top Position of Element With a Click

In this example, we will have a div that will be a box. We will let the user check the top position of the div and also let the user add margin to the top of the div to change its top position.

Here is the simple HTML set up:

<div id="div1">
  <div id="box" style="height: 60px; width:60px; background: #7bbfa2; margin-top: 0px;"></div>
  <p id="result"></p>
  <div id="click-me1" class="click-me" onclick="getTopPosition()">Get top position</div>
  <div id="click-me2" class="click-me" onclick="addMargin()">Add margin</div>
</div>

In the JavaScript code, when a user clicks a button, we will run a function that we will create. We do this with the help of two onclick events that are attached to each button.

In the first function, we will get the top position of the element by using the offsetTop property to return the top position of the element. We will show this number to the user using the textContent property.

In the second function, we will simply get the top margin of the div, and add 20px to it. We will do this using the marginTop property.

Here is the JavaScript code:

function getTopPosition(){
  var topPosition = document.getElementById('box').offsetTop;
  document.getElementById('result').textContent = topPosition;
};
function addMargin(){
  var marginTop = document.getElementById('box').style.marginTop;
  //The number we get will have px at the end. This code removes it
  var topNumber = Number(marginTop.substring(0,marginTop.indexOf('px')));
  document.getElementById('box').style.marginTop = ((topNumber + 20) + 'px');
};

The final code and output for this example is below:

Code Output:

Get top position
Add margin

Full Code:

<div id="div1">
  <div id="box" style="height: 60px; width:60px; background: #7bbfa2; margin-top: 0px;"></div>
  <p id="result"></p>
  <div id="click-me1" class="click-me" onclick="getTopPosition()">Get top position</div>
  <div id="click-me2" class="click-me" onclick="addMargin()">Add margin</div>
</div>

<script>

function getTopPosition(){
  var topPosition = document.getElementById('box').offsetTop;
  document.getElementById('result').textContent = topPosition;
};
function addMargin(){
  var marginTop = document.getElementById('box').style.marginTop;
  //The number we get will have px at the end. This code removes it
  var topNumber = Number(marginTop.substring(0,marginTop.indexOf('px')));
  document.getElementById('box').style.marginTop = ((topNumber + 20) + 'px');
};

</script>

Hopefully this article has helped you understand more about JavaScript offsetTop.

Other Articles You'll Also Like:

  • 1.  JavaScript join – Create String from Elements in an Array
  • 2.  Using JavaScript to Create a Unique Array
  • 3.  Convert String to Float in JavaScript
  • 4.  Using JavaScript to Remove Leading Zeros
  • 5.  Reverse a String in JavaScript
  • 6.  How to Split a Number into Digits in JavaScript
  • 7.  How to Empty a String in JavaScript
  • 8.  Creating a JavaScript Function to Add Two Numbers
  • 9.  Remove the Last Element From Array in JavaScript
  • 10.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy