Ways to obtain the height of a UL element while the display property is configured to flex

While I was blockwise segregating elements of a webpage and drawing borders around those blocks, I encountered an issue where the height turned out to be 0 when the display was set to flex. How can I retrieve the height in such scenarios? I experimented with various methods but they all yielded 0

ul.offsetHeight // yields 0
ul.clientHeight // yields 0
ul.getBoundingClientRect() // also gives me 0
window.getComputedStyle(ul).height // shows 100%

//Even this method returns 0
var range = document.createRange()
range.selectNodeContents(ul)
range.getBoundingClientRect()

UPDATE

Turns out, the issue had nothing to do with the flex property. The height displayed as 0 simply because the element was not visible on the screen at that moment. I don't wish to delete the question since some people have already devoted their time to it.

Answer №1

Have you considered the possibility that your element is being dynamically created? It's important to ensure that you're not trying to retrieve its height before it has actually been created. In my experience, assigning an id to the div and accessing its height property directly usually works without any issues. So, double check whether you are fetching the data too early or if the height has been inadvertently set to 0 at some point.

If your goal is to target multiple ul elements simultaneously rather than a specific one, make sure to obtain the height of each item in the array individually, such as ul[0].

//Single Call
document.getElementById('one').innerHTML = document.getElementById('someDiv').clientHeight;

//Multi Call
var multi = document.getElementsByTagName('ul');
document.getElementById('two').innerHTML = multi[1].clientHeight;
ul {
  background: #f00;
  display: flex;
  flex-direction: column;
}
<p>
  <ul id="someDiv">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
  </ul>
</p>
<p>
  <ul id="anotherDiv">
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
  </ul>
<p>
<p>First ul Height: <span id="one"> - </span></p>
<p>Second ul Height: <span id="two"> - </span></p>

Answer №2

I attempted the following method:

document.write(document.querySelector('ul').offsetHeight);
ul {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
}
<ul>
  <li>a</li>
  <li>a</li>
  <li>a</li>
  <li>a</li>
</ul>

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

Unable to retrieve custom CSS and JS files hosted on the server

Encountering Server Issue My server is returning a 404 NOT FOUND error when trying to access my static files: css and js. In IntelliJ IDEA, the path appears correct as shown in the image https://i.stack.imgur.com/nTFv9.png. However, upon accessing the pa ...

Implementing a Vue.js search function with two v-for loops

I have computed the following filter: <script> export default { data() { return { columns:{}, search:"", trovato:{}, }; }, computed: { ...

I need to obtain the URL pathname on a server component in Next.js version 13 - how is this achieved

I'm facing an issue with retrieving the pathname of a server component located in the app directory. Despite attempting to obtain it through window.location, I haven't been successful. Is there an alternative method I can use to achieve this? ...

Can lazy loading be implemented for the video tag using JavaScript?

Currently, I am in the process of working on a project that entails incorporating three videos onto the homepage. However, loading them simultaneously is causing a significant decrease in load time. In addition, I prefer to utilize the <video/> tag ...

Stop users from being able to input line breaks by pasting

I am currently facing a challenge with my code. Within the code, I have included a textarea where users can input the title of an article, and I want this title to be restricted to only one line. To achieve this, I created a script that prevents users from ...

Loading indicator for buttons

Issue with submit button onclick function (onClick="mandatoryNotes()"). It is taking a while to load, so a preloader script was added. The preloader is now working, but the function is not being called. Seeking assistance. Thank you. function mandatoryN ...

Session variable not refreshing post AJAX query

I'm facing a bit of a puzzle here. I have 2 php pages in play. One acts as the template display, and upon clicking, an AJAX call fetches an external page to show all necessary information. The issue arises with my session variable not updating as expe ...

The load event in React's iframe is failing to fire after the src attribute is modified using state

In the process of creating a registration form for a React application, we encountered the need to incorporate an HTML legal agreement as an iframe. This legal document is available in various languages, one of which can be selected using a drop-down menu; ...

Effortlessly Transform HTML into Plain Text Using jQuery

If I wanted to create a feature on my website where users can input text into a text area and then convert it to HTML by clicking a button, how could I achieve that? I'm looking for functionality similar to what Wordpress offers, where users can enter ...

Achieving the Low Poly Aesthetic with Three.js

I'm attempting to generate terrain in the low poly style, characterized by each triangle being one shade. However, my efforts have resulted in very smooth shading. An example with a few triangles can be seen here: https://i.sstatic.net/cZSBe.png ( ...

Create a function called plagiarismChecker that requires two parameters: an array containing the students' responses, and a small snippet of text

Write a custom function called detectPlagiarism that accepts two parameters: an array of student responses and a snippet of text to check for plagiarism. The function should return true if any of the essay question answers contain the specified text, and f ...

jQuery document.ready function not triggering as expected (even with jQuery included)

After conducting thorough research on Google and Stack Overflow, I noticed a common issue with incorrectly including jQuery. However, I made sure to include it correctly. In my header, I have the following code: <!DOCTYPE html PUBLIC "-//W3C//DTD ...

Add new content to an existing JSON file in a Node.js environment

I'm encountering an issue while attempting to insert new text into an existing JSON file. I've experimented with writeFileSync and appendFileSync methods, however the added text does not maintain JSON formatting even when utilizing JSON.stringify ...

Is there a way to set the image result retrieved from the API call as the background image for my app?

In my current project, the user is able to input a city and the background of the page changes to display an image of that specific city. This image is obtained through an API call to Pixabay API, with the result stored in a variable named "background". Ho ...

Tips for displaying a continuously increasing number on a webpage without the need to refresh the page

When you deploy the webpage, a number will start increasing. Let's say it starts at 1 and grows to 10000, incrementing by 2 or any calculated number every minute. Each time you visit the page, you will see the current number displayed. For instance, i ...

jQuery form validation is experiencing issues with ajax not responding

I've been working on code validation using jQuery and I've run into an issue with AJAX. The page is not loading properly, and I suspect there may be an issue with the file path or something else. Any help in resolving this would be greatly apprec ...

Guide to fetching and displaying dropdown values from a database using PHP

Hello everyone, I need help on retrieving data from a database and displaying it in a drop-down list. I have written the following PHP code: public function System_parameter_dropdown_value() { $sql= " SELECT * FROM SYSTEM_PARAMETERS WHERE PARAMETER_KE ...

What is the process for utilizing JavaScript to parse a JSON file stored locally?

I am trying to retrieve data from a JSON file located locally using JS/jQuery. Despite finding numerous similar questions on Stack Overflow, I have not found the right solution. All answers seem to give me a jquery.min.js:4 XMLHttpRequest cannot load file ...

The Architecture of a Node.js Application

I'm curious about the efficiency of my nodejs app structure in terms of performance optimization. My main concern lies in how I handle passing around references to my app object across modules. Basically, in my app.js file, I define all my dependenci ...

What is the best way to conceal overflowing text with ellipsis by utilizing dynamic Bootstrap columns?

I'm having trouble aligning a text alongside an icon within a card. The card is enclosed in the Bootstrap "col" class, which dynamically adjusts its size based on the number of items in a row. When there isn't enough space, the text wraps and ap ...