Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Reading Original Div height using JS

    I have a Div that goes about 5 lines, or any variable length downward. Then I set the height to 16px using

    Code:
    <div style='height: 16px'>a<br>c<br>d<br>e<br>g</div>
    works fine, but I need to find the original height before it is set using javascript.

    .innerHeight reads the new value, and so does the other one I tested (I forgot what it is called).

    So I thought I would set it back to it's original height by setting the style.height to null, works fine in FF, IE, and Chrome, but not Opera. I prefer a "hack-less" method, and one that works in all browsers, and the only idea I can think of is using javascript to set the height after it has been created, and read it then like:

    Code:
    <div id="test">a<br>c<br>d<br>e<br>g</div>
    <script type="text/javascript">setHeight("test", 16)</script>
    This code is pretty inefficient, a hassle, and not very elegant.

    Is there an alternate way to read the original height or a cleaner and elegant way of writing the post div javascript setHeight code?

    Thanks
    Arun
    Last edited by arunforce; 07-24-2009 at 04:15 AM.

  • #2
    Regular Coder Amphiluke's Avatar
    Join Date
    Jul 2009
    Posts
    312
    Thanks
    3
    Thanked 89 Times in 89 Posts
    In the head section:
    Code:
    <script type="text/javascript">
       var originalDivHeight = 0;
       window.onload = function() {
          var testDiv = document.getElementById("test");
          if (testDiv) {
             originalDivHeight = testDiv.offsetHeight;
             testDiv.style.height = "16px";
          }
       }
    </script>
    In the body section:
    Code:
    <div id="test">a<br>c<br>d<br>e<br>g</div>
    Does it suit you?
    I am still learning English

  • Users who have thanked Amphiluke for this post:

    arunforce (07-23-2009)

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I'll give it a try, thanks.

    I didn't know header would be JS would be executed after the body is loaded.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •