1

just a basic question

I have an object and I'm trying to get the css property out of it. So what I want is like this

var width = e.style.width;

I'm like 90% sure that width would become a string, but then again I'm not really sure. I can set the the objects with

e.style.width = 200;

so it places an int or float or w/e value I want but not sure how to get the css style out.

Thanks

wzsun
  • 295
  • 2
  • 7
  • 15
  • parseFloat do the job, but you will have troubles with percentages, the answer from @koder will be more usefull for a better code. – Gabriel Gartz Jan 03 '13 at 16:25

3 Answers3

3

you should use as following

e.style.width = '80.2%';
e.style.width = '80.2em';

Please see following links Link 1 link 2

and if you try get width using javascript then you could use as following code

parseFloat(e.style.width);
Community
  • 1
  • 1
2

Just use

parseFloat(e.style.width);

This will strip out the "px" out of the value and give you the actual value in pixels.

ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
  • 1
    And will also do "fun stuff" for a value of `100%`, should such be encountered .. –  Jan 02 '13 at 06:18
1

For getting object width you can use clientWidth or offsetWidth as (both will return you an integer):

var width = e.clientWidth;

or

var width = e.offsetWidth;

and to set object width you can use the following code:

e.style.width = "200px";
koder
  • 454
  • 6
  • 18