Solved

Need help with getting height of a div...

Posted on 2010-11-24
5
675 Views
Last Modified: 2013-11-11
Hi,

I'm trying to programmatically / dynamically get the height of a div (containing data that dynamically increases the height of the div) in pixels using javascript. Based the javascript code below, the alert always shows a div height of 0. But if I specifically set a fixed height for the div (for example,  <div id="sidebar" style="height:150px;width:500px;border:1px solid #ccc;padding:10px;">), then the alert shows the correct fixed height of the div, which is 150. How do I get the dynamic height in pixels?

    <div id="sidebar" style="width:500px;border:1px solid #ccc;padding:10px;">
        <div id="leftNav">
            <div id="product">
                        Products
                  </div>            
            <div id="productList">
                <ul>
                              <li>Product 1</li>
                              <li>Product 2</li>
                              <li>Product 3</li>
                </ul>
            </div>            
            <div id="service">
                        Services
                  </div>
            <div id="serviceList">
                <ul>
                              <li>Service 1</li>
                              <li>Service 2</li>
                              <li>Service 3</li>
                </ul>
            </div>
        </div>                                  
   </div>
<script type="text/javascript">
      var h = document.getElementById('sidebar').style.pixelHeight;
      alert('div height = ' + h);
</script>

Many thanks in advance.
0
Comment
Question by:WebAppDeveloper
  • 3
  • 2
5 Comments
 
LVL 2

Expert Comment

by:Atr3ides
ID: 34208602
Try this
var h = document.getElementById('sidebar').offsetHeight;
alert('div height = ' + h);
0
 

Author Comment

by:WebAppDeveloper
ID: 34208627
Atr3ides,

document.getElementById('sidebar').offsetHeight; does NOT work neither.  The alert says "div height = undefined"
0
 

Author Comment

by:WebAppDeveloper
ID: 34208637
I've  tried all these, but they don't work:

var h = document.getElementById('sidebar').height;
var h = document.getElementById('sidebar').pixelHeight;
var h = document.getElementById('sidebar').clientHeight;
var h = document.getElementById('sidebar').offsetHeight;

0
 
LVL 2

Accepted Solution

by:
Atr3ides earned 500 total points
ID: 34208697
Here's the result of using those, with your HTML, in FF and Chrome, with Firebug/Dev tools active

 
<div id="sidebar" style="border:1px solid #ccc;padding:10px;">
        <div id="leftNav">
            <div id="product">
                        Products
                  </div>            
            <div id="productList">
                <ul>
                              <li>Product 1</li>
                              <li>Product 2</li>
                              <li>Product 3</li>
                </ul>
            </div>            
            <div id="service">
                        Services
                  </div>
            <div id="serviceList">
                <ul>
                              <li>Service 1</li>
                              <li>Service 2</li>
                              <li>Service 3</li>
                </ul>
            </div>
        </div>                                  
   </div>
<script type="text/javascript">
    var h = document.getElementById('sidebar').height;
    console.log('div height = ' + h);
    var h = document.getElementById('sidebar').pixelHeight;
    console.log('div height = ' + h);
    var h = document.getElementById('sidebar').clientHeight;
    console.log('div height = ' + h);
    var h = document.getElementById('sidebar').offsetHeight;
    console.log('div height = ' + h);
</script>

Open in new window


Firefox:
div height = undefined
div height = undefined
div height = 244
div height = 246

Open in new window


Chrome:
div height = undefined
test.html:29div height = undefined
test.html:31div height = 244
test.html:33div height = 246

Open in new window

0
 

Author Comment

by:WebAppDeveloper
ID: 34208782
Suddenly, the following are now working for me.

var h = document.getElementById('sidebar').clientHeight;
var h = document.getElementById('sidebar').offsetHeight;

Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now