[Javascript] Getting the width property of div

Shawn Milo shawn.milo at gmail.com
Tue Apr 5 10:45:02 CDT 2005


Wow, who would have thought that this would be such a complex question?
I still don't have the answer, but after some playing around and Google Groups
searching, I've put together some stuff which should help you find the answer.

Please report back to the group when/if you find the solution you're
looking for.
The code I'm attaching is tested and working, but some parts work only in IE, 
some only in FireFox, and I'm getting different values from different
lines... :o/

Shawn

code follows:
--------------------------------------------------------------------------------------------

   function widthTest(someObject){

      if (someObject.style.width){
         alert('style.width: ' + someObject.style.width);
      }

      if (someObject.style.pixelWidth){
         alert('style.pixelWidth: ' + someObject.style.pixelWidth);
      }

      if (someObject.offsetWidth){
         alert('.offsetWidth: ' + someObject.offsetWidth);
      }

      if (document.defaultView && document.defaultView.getComputedStyle) {
          alert('computedStyle: ' +
document.defaultView.getComputedStyle(someObject,'').getPropertyValue('width'));
      }

   }


--------------------------------------------------------------------------------------------

On Apr 5, 2005 11:03 AM, Alan Gardner <gardner.alan at gmail.com> wrote:
> Can someone help me, I'm having trouble getting the width of a div
> tag. I thought this would be a fairly easy syntactically, but all I
> get is an empty string.
> 
> my CSS:
> #div1 { width:400px };
> 
> my JS:
> function getWidth() {
>         alert(document.getElementById('div1').style.width);
> }
> 
> How do I find out the current width of a div?
> THA
> Alan
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript
> 


-- 
Voicemail any time at:
206-666-MILO



More information about the Javascript mailing list