jQuery's height calculations for elements are inaccurate

I have a total of seven divs, each with the class "row". Initially, their content is hidden but there is a timeline that spans from the first row to the last one. The height of the timeline is calculated dynamically based on the combined heights of all rows. Here's the code snippet:

<div id="milestone_view" class="timeline">
  <div id="line" ></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
  <div class="row"></div>
</div>

Here's the JavaScript code:

var totalHeight = 0;
$("#milestone_view").children('.row').each(function(){
    totalHeight += $(this).height();
    $(this).attr('height',$(this).height());
});
$("div#line").css({height:(totalHeight)});
alert(totalHeight);

I included

$(this).attr('height',$(this).height());
to monitor each div's height. Although it seems simple, when the page loads, I receive an alert saying "363". Six divs have a height of 51 and one has 57 (slightly larger which is fine). However, when I click on any div to show its content, and then hide it by clicking again (triggering the function), I get "419". Now, six divs have a height of 59px and one has 65px. It appears that their initial height calculation is incorrect.

I hope my explanation was clear. This is just a small part of an application developed in kendoui, but currently facing issues. Worth noting is that these divs are loaded dynamically, not just their content. Also, the number of divs can vary, possibly more or less than seven.

Answer №1

It appears that the .row elements are surrounded by a 8px margin. To account for this margin, replace height() with outerHeight() in your code:

totalHeight += $(this).outerHeight(true);

Answer №2

Seems like there are certain styles like padding, margin, border, or line height that are affecting your hidden elements only when they become visible, causing the discrepancy in appearance before and after display.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

iOS Safari does not support CSS transforms when applied to parent elements

As a workaround, I decided to create a mockup of the scenario since developing a testable version wasn't straightforward. However, here is the gist of the issue: @keyframes mainFadeIn { 0% { opacity: 0; transform: translateY(-3rem); } ...

Padding on a flex item nudges a neighboring flex item out of place

Encountering an issue - within a div container, I have 4 nested divs using the grid system. Oddly enough, the third div in the grid is not behaving as expected. When setting a margin-bottom for this div, instead of creating space at the bottom, it pushes ...

unusual behavior when using the CSS property transform: rotate

i'm attempting to enable this element to be both draggable and rotatable. However, when I apply transform:rotate(0deg);, I am able to drag it anywhere within the parent container. But when I set it to 90deg, there are certain areas that become undrag ...

"Improve your Angular ngrx workflow by utilizing the sandbox pattern to steer clear of

Currently, I'm trying to determine whether my implementation of the ngrx and sandbox pattern is effective. Here's the issue I'm facing: getFiles(userId: number, companyId: number) { this.fileService.getFiles(userId, companyId).subscribe(re ...

Click once to expand all rows in the table with ease

I have successfully created a basic table using HTML. The table includes nested elements that are designed to open individually when clicked on the assigned toggle. Essentially, clicking on a '+' icon should reveal a sub-menu, and clicking on a & ...

My goal is to store the received response in an array within an object

const data = [ { id: 1, user_name: 'john', phone_number: 5551234567 }, { id: 2, user_name: 'jane', phone_number: 5559876543 }, { id: 3, user_name: 'doe', ...

What is the process for generating a watermark directive in angularjs?

My application utilizes a jQuery script for watermarking textboxes. I created a directive to easily apply this watermark to input elements, but it's not functioning as expected. While debugging, I can see the watermark being added, but once the UI fin ...

When working on a REST APIs project with ReactJS fetch and NodeJS, I am encountering difficulties in reading authorization headers on the server side

I'm having issues retrieving headers on the server side using ReactJS fetch to call an API on the front end. In my old project, this functionality was working perfectly fine. I'm puzzled as to why it's not working now, especially since I hav ...

Is there a way to adjust the brightness of specific sections within an image using html, css, and js?

I am working on a project where I have an image with tags underneath that correspond to different parts of the image. For example, if the image is of a dog, one of the tags could be 'nose', indicating the portion of the image around the dog' ...

Using Xpath to target elements based on attributes and retrieving the values of another attribute

I am trying to extract the datetime attribute value from each li element that contains an attribute type with the value of standard. However, my current code is resulting in an error. Here is the snippet: <li datetime="2019-06-03T14:45" offset="-135" t ...

Executing password validation on login/register form using Node.js and EJS

In order to demonstrate a simple login page, I have created a form that requests typical information like username, password, etc. Additionally, it prompts the user to confirm their password, and if the lengths do not match, an error is triggered to notify ...

What's inside the navigation bar, icons, and content cards? (Using HTML and Bootstrap)

Having a few issues that need resolving: Unable to display the contents of my navbar after the navbar-brand section. Looking to add a home button, about section with a drop-down menu including: 'Our mission', 'our team', & ...

Aligning text to the center and having it displayed on either side of it

Hey there, I need help with the following: <div id='1'></div> <div id='2'></div> <div id='3'></div> I want to place a single word into each div. The width of each div should be ...

The height of the Material UI dropdown is displaying in an improper manner

I'm currently experimenting with the Material UI dropdown on my website, utilizing the react-select library. However, I've encountered an issue where the UI gets compromised when using an option that exceeds the width of the dropdown. If anybody ...

Utilizing the dynamic capabilities of Ajax in tandem with the popular Jquery and Code

tag, I have a scenario where a controller is sending data from two functions in a model to a view. This involves retrieving the results of 'waiting' and 'beingseen' functions and passing them to the 'studentqueue/studentqueue' ...

As the second line of Javascript code is being executed, the first line is still

I have a task where I need to execute a SQL SELECT statement and save the results. Then, those results need to be passed into a function to generate a graph using the data points provided. Refer to the code snippet below for details. var dataKWhr = getCov ...

Cufon failing to load following an ajax call

At first, cufon is used to replace the text on the main page. However, when another page file is loaded, cufon does not apply its replacement to the newly loaded content. Why is that? I tried adding cufon.refresh(); as the last function in the chain. I c ...

Rotate the mat-select arrow when the dropdown opens (moving in both upward and downward directions)

Currently, I have a mat-select dropdown icon arrow that toggles facing up or down based on user clicks. However, after selecting an option and closing the dropdown, the arrow remains in the upward position unless further clicked and held. I have attempted ...

What is the best way to dynamically insert columns into HTML code?

This is an example of my HTML code: <div class="row text-center"> <div class="col h4">We Collaborate With:</div> <div class="col">company1</div> <div class="col">company2</div> ...

Experiencing problems with website loading due to jquery?

Recently, I've been experiencing slow loading times on my website . It's taking about a minute for the site to load properly, but strangely, if I click on the stop loading button, the site loads instantly. Does anyone have any insight into what ...