wood burning stoves 2.0*
The moose likes JSP and the fly likes get current width of DIV element in JavaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "get current width of DIV element in JavaScript" Watch "get current width of DIV element in JavaScript" New topic
Author

get current width of DIV element in JavaScript

Nicholas Turner
Ranch Hand

Joined: May 24, 2003
Posts: 126
how do I get the current width of a DIV element
posWidth method gets the original value and not the current.
thanks,
nick
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Can't be done. The width value is never set or reset when the browser reflows the display.
hth,
bear
P.S. HTML and JavaScript question are better asked in the HTML/JavaScript forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nicholas Turner
Ranch Hand

Joined: May 24, 2003
Posts: 126
you know I looked for that forum but didnt see it
Chris Baron
Ranch Hand

Joined: Mar 21, 2003
Posts: 1049
Sorry Bear, it's possible. OffsetWidth is originally a window attribute in MS JScript, but it can be used in common DOM Browsers for other objects:

Tested in IE and Phoenix ( now Firebird, Mozilla )
Edit: Just found out, that it even returns a pixel-value when you define a percentage width in the style attribute
[ June 08, 2003: Message edited by: Chris Baron ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Interesting. I got some weird behavior for this attribute under Mozilla on Windows (but not on the Mac), but otherwise it seems to work on most of the major browsers.
It's not part of the W3C DOM spec, so you might want to be careful using it if you need broad browser support.
Here's more info.
So, I'll amend my answer to: there's no standards-compliant way to do it that works on all W3C-compliant browsers. But if it works in a subset of browsers that you are interested in, go for it.
hth,
bear
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

The HTML/Javascript forum is here.
hth,
bear
Nicholas Turner
Ranch Hand

Joined: May 24, 2003
Posts: 126
I must have had a brain freeze, or saw a moose
 
wood burning stoves
 
subject: get current width of DIV element in JavaScript
 
Similar Threads
Parent div not expanding when elements in child div expands
Failed to Set the Height and Width of a Surrounding Box Using DIV
How view of Table can be control with the help of Array Elements
AJAX Dialog Windows
need to show part of the <td> as a <span> of a different color.