• 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 Position of Element

Using jQuery to Get the Position of Element

April 4, 2022 Leave a Comment

We can use jQuery to get the 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 and left coordinates from this method.

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

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

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

Using jQuery to Get the Position of an Element With a Click

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

Here is the simple HTML set up:

<div id="div1">
  <div class="box" style="height: 60px; width:60px; background: #7bbfa2;"></div>
  <p id="result"></p>
  <div id="click-me1" class="click-me">Get 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 position of the element by using the jQuery offset() method to return the coordinates of the element, and then get the top and left coordinates. We will show these numbers to the user using the text() method.

In the second function, we will simply get the top and left margins of the div, and add 20px to each. We will do this using the css method.

Here is the jQuery code:

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

The final code and output for this example of how to use jQuery to get the position of an element is below:

Code Output:

Get position

Add margin

Full Code:

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

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  jQuery val Method – Get the Value from an Input Field
  • 2.  jQuery width() – Using jQuery to Get the Width of a Div
  • 3.  jQuery before – Insert HTML Before Another Element
  • 4.  Using jQuery to Empty the Contents of an HTML Element
  • 5.  jQuery insertBefore – Insert Element Before Another Element
  • 6.  Using jQuery to Change the Id of a Div
  • 7.  Using jQuery to Add Required Attribute to Input Field
  • 8.  Using jQuery to Capitalize the First Letter of a String
  • 9.  Using jQuery to Get the Previous Sibling of an Element
  • 10.  jQuery slideUp – How to Slide Up and Hide an Element Using jQuery

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