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

Using jQuery to Get the Bottom Position of Element

March 28, 2022 Leave a Comment

We can use jQuery to get the bottom 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. To get the bottom position of the element, we will have to add the height of the element to this number, using the height() method.

var bottom = $('#div1').offset().top + $('#div1').height();

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

var bottom = jQuery('#div1').offset().top + jQuery('#div1').height();

Using jQuery to Get the Bottom 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 bottom position of the div and also let the user add margin to the top of the div to change its bottom 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 bottom 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 then get the height of the div, and add this number to our top coordinate, to get the bottom value of the div.

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 bottomPosition = $('.box').offset().top + $('.box').height();
  $('#result').text(bottomPosition);
});
$('#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 bottom 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 bottom position</div>
  <div id="click-me2" class="click-me">Add margin</div>
</div>

<script>

$('#click-me1').click(function(){
  var bottomPosition = $('.box').offset().top + $('.box').height();
  $('#result').text(bottomPosition);
});
$('#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 bottom position of an element.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Show and Hide a Div
  • 2.  Using jQuery to Delete an Element
  • 3.  How to Use jQuery to Remove Element
  • 4.  jQuery Display None
  • 5.  Using jQuery to Remove Background Color
  • 6.  jQuery focus – Changing Form Styles with the focus() Method
  • 7.  jQuery rotate – How to Rotate an Image using jQuery
  • 8.  jQuery hover – An Interactive Example of the hover Method
  • 9.  Get nth Child with jQuery
  • 10.  Using jQuery to Get the Current URL

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