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

1
  • parseFloat do the job, but you will have troubles with percentages, the answer from @koder will be more usefull for a better code. Commented Jan 3, 2013 at 16:25

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

And will also do "fun stuff" for a value of 100%, should such be encountered ..
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";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.