Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. How do I query for an element's size in Javascript?

How do I query for an element's size in Javascript?

Scheduled Pinned Locked Moved Web Development
questionjavascriptdatabase
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dave Calkins
    wrote on last edited by
    #1

    I have an element on my web page. I'd like to discover the size of the element from within Javascript code. var eleHeight = document.getElementById('myelement').style.height; alert('the height is ' + eleHeight); The alert just shows 'the height is '. What am I doing wrong? I'm able to set the height of an element, but I don't seem to be able to read it. I'd like to position elements absolutely using their size. Without being able to query for their size it'll be tough to do this.

    M S 2 Replies Last reply
    0
    • D Dave Calkins

      I have an element on my web page. I'd like to discover the size of the element from within Javascript code. var eleHeight = document.getElementById('myelement').style.height; alert('the height is ' + eleHeight); The alert just shows 'the height is '. What am I doing wrong? I'm able to set the height of an element, but I don't seem to be able to read it. I'd like to position elements absolutely using their size. Without being able to query for their size it'll be tough to do this.

      M Offline
      M Offline
      Michael Sync
      wrote on last edited by
      #2

      Did you set the height for your element? <div id="myelement" style="height:100px;" />

      Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

      D 1 Reply Last reply
      0
      • D Dave Calkins

        I have an element on my web page. I'd like to discover the size of the element from within Javascript code. var eleHeight = document.getElementById('myelement').style.height; alert('the height is ' + eleHeight); The alert just shows 'the height is '. What am I doing wrong? I'm able to set the height of an element, but I don't seem to be able to read it. I'd like to position elements absolutely using their size. Without being able to query for their size it'll be tough to do this.

        S Offline
        S Offline
        Shog9 0
        wrote on last edited by
        #3

        Check out the clientHeight[^] and offsetHeight[^] properties. With a bit of thought, you can probably make them work for what you need. As Michael mentioned, you'll need to set an explicit height in order to use the style.* properties.

        ----

        ...the wind blows over it and it is gone, and its place remembers it no more...

        D 1 Reply Last reply
        0
        • M Michael Sync

          Did you set the height for your element? <div id="myelement" style="height:100px;" />

          Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

          D Offline
          D Offline
          Dave Calkins
          wrote on last edited by
          #4

          no, there is no size set for the element. at runtime, I fill it in with text and its size changes based on how much text is used. I need to find out dynamically what the actual size turned out to be.

          M 1 Reply Last reply
          0
          • S Shog9 0

            Check out the clientHeight[^] and offsetHeight[^] properties. With a bit of thought, you can probably make them work for what you need. As Michael mentioned, you'll need to set an explicit height in order to use the style.* properties.

            ----

            ...the wind blows over it and it is gone, and its place remembers it no more...

            D Offline
            D Offline
            Dave Calkins
            wrote on last edited by
            #5

            Thanks! The clientHeight did the trick and seems to have worked in both IE and FireFox :) Adding more text to the element, which caused it to grow, resulted in the larger size being provided in clientHeight. Interestingly, I have the O'Reilly Javascript book and it says nothing about this property. I'm not complaining of course, as the code is now working :)

            1 Reply Last reply
            0
            • D Dave Calkins

              no, there is no size set for the element. at runtime, I fill it in with text and its size changes based on how much text is used. I need to find out dynamically what the actual size turned out to be.

              M Offline
              M Offline
              Michael Sync
              wrote on last edited by
              #6

              Dave Calkins wrote:

              there is no size set for the element. at runtime

              If you don't set the height explicitly, you will get nothing from style.height. So, As Slang suggested, try to use clientHeight and offsetHeight..

              Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

              D 1 Reply Last reply
              0
              • M Michael Sync

                Dave Calkins wrote:

                there is no size set for the element. at runtime

                If you don't set the height explicitly, you will get nothing from style.height. So, As Slang suggested, try to use clientHeight and offsetHeight..

                Thanks and Regards, Michael Sync ( Blog: http://michaelsync.net)

                D Offline
                D Offline
                Dave Calkins
                wrote on last edited by
                #7

                Thanks, yes, clientHeight worked.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups