• 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 / jQuery / Using jQuery to Get the Top Position of Element

Using jQuery to Get the Top Position of Element

March 27, 2022 Leave a Comment

We can use jQuery to get the top position of an element easily using the offset() method.

$('#div1').offset();

The offset method will return the coordinates of the element relative to the page. We can then get the top coordinate from this method.

$('#div1').offset().top;

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery('#div1').offset().top;

Using jQuery 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:

<style>.box{ height: 60px; width:60px; background: #7bbfa2; }</style>
<div id="div1">
  <div class="box"></div>
  <p id="result"></p>
  <div id="click-me1" class="click-me">Get top position</div>
  <div id="click-me2" class="click-me">Add margin</div>
</div>

In the JavaScript code, when a user clicks a button, we will run a function that we will create.

In the first function, we will get the top position of the element by using the jQuery offset() method to return the coordinates of the element, and then get the top coordinate. We will show this number to the user using the text() method.

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 css method.

Here is the jQuery and JavaScript code:

$('#click-me1').click(function(){
  var topPosition = $('.box').offset().top;
  $('#result').text(topPosition);
});
$('#click-me2').click(function(){
  var marginTop = $('.box').css('margin-top');
  //The number we get will have px at the end. This code removes it
  var topNumber = Number(marginTop.substring(0,marginTop.indexOf('px')));
  $('.box').css('margin-top', ((topNumber + 20) + 'px'));
});

The final code and output for this example is below:

Code Output:

Get top position

Add margin

Full Code:

<style>.box{ height: 60px; width:60px; background: #7bbfa2; }</style>
<div id="div1">
  <div class="box"></div>
  <p id="result"></p>
  <div id="click-me1" class="click-me">Get top position</div>
  <div id="click-me2" class="click-me">Add margin</div>
</div>

<script>

$('#click-me1').click(function(){
  var topPosition = $('.box').offset().top;
  $('#result').text(topPosition);
});
$('#click-me2').click(function(){
  var marginTop = $('.box').css('margin-top');
  var topNumber = Number(marginTop.substring(0,marginTop.indexOf('px')));
  $('.box').css('margin-top', ((topNumber + 20) + 'px'));
});

</script>

Hopefully this article has helped you understand how to use jQuery to get the top position of an element.

Other Articles You'll Also Like:

  • 1.  jQuery slideUp – How to Slide Up and Hide an Element Using jQuery
  • 2.  Using jQuery to Change Div Text
  • 3.  Using jQuery to get Textarea Value
  • 4.  jQuery insertBefore – Insert Element Before Another Element
  • 5.  jQuery parent – Get the Parent Element of a Div
  • 6.  How to Use jQuery to Remove Element
  • 7.  Using jQuery to Get Element by ID
  • 8.  Using jQuery to Get the Parent Div of an Element
  • 9.  jQuery Display None
  • 10.  jQuery delay – How to Delay the Start of a Method

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