Home > Web Front-end > JS Tutorial > body text

How to Get the Height of a Hidden Element in jQuery?

Patricia Arquette
Release: 2024-11-03 21:15:29
Original
320 people have browsed it

How to Get the Height of a Hidden Element in jQuery?

Getting the Height of Hidden Elements in jQuery

When working with hidden elements, it can be challenging to obtain their dimensions. Many developers resort to revealing the element temporarily, measuring its height, and then concealing it again. However, there is a more effective approach.

jQuery provides a convenient solution for this task. Here's how you can get the height of a hidden element within a concealed parent div:

<code class="javascript">var previousCss = $("#myDiv").attr("style");

$("#myDiv").css({
    position:   'absolute', // Optional if #myDiv is already absolute
    visibility: 'hidden',
    display:    'block'
});

optionHeight = $("#myDiv").height();

$("#myDiv").attr("style", previousCss ? previousCss : "");</code>
Copy after login

This approach involves setting the element's position to "absolute" (optional if it's already absolute), making it invisible, and temporarily unhiding it. You can then measure its height and restore its previous style attributes. This method allows you to work with hidden elements without disrupting the page's layout or causing unnecessary flickering.

The above is the detailed content of How to Get the Height of a Hidden Element in jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!