views:

49

answers:

2

When reading width of DIV section, the result comes in %. But, I want to convert it in pixel value. How is it possible ?

I have a class "h" defined in my html. It has width="100%". It's parent div has width="69%".

width = ($(this).find('.h').width())

So actually the width is not window.width() but it is 69% of it. I want it in pixel !!!

+2  A: 

From what I read on the jQuery API , you can use .width() in jQuery to return a unitless pixel value that can be used for mathematical calculations.

Example:

$(window).width();   // returns width of browser viewport
$(document).width(); // returns width of HTML document

Click the link and look at the demo at the bottom of the first function. I think it's your answer.

rownage
+6  A: 

Instead of reading the css use .width(), in the case the div isn't visible (which appears to be the case from your previous answer), show it get the width and hide it, like this:

var width = $("#div").show().width(); $('#div").hide();

since the UI thread won't update until this is done...the user won't see it. Make sure .width() is what you're after though (e.g. do you want margins or not?) There are other dimension methods if you do/don't want padding/margins, you can find them here.

Nick Craver
Quick Draw Craver beat me to it.
rownage