get height of DIV

  • Thread starter samuelberthelot
  • Start date
S

samuelberthelot

Hi,
To get the height of my DIV, I do :

myDivElement.style.height, which returns "176px". I don't want that. I
want to get 176. How could I do that, which property should I use? (I
could do a string parsing, but what a pain....)

Thanks
 
W

web.dev

Hi,
To get the height of my DIV, I do :

myDivElement.style.height, which returns "176px". I don't want that. I
want to get 176. How could I do that, which property should I use? (I
could do a string parsing, but what a pain....)

Thanks

There is no property which would return you just the number, you will
have to do string parsing. One way of doing this could be the
following:

//assuming you have your element

var height = myDiv.style.height;
height = height.split("px")[0];
 
S

samuelberthelot

Thanks for the reply. I thought I could avoid having to do that... ok
then.
web.dev said:
Hi,
To get the height of my DIV, I do :

myDivElement.style.height, which returns "176px". I don't want that. I
want to get 176. How could I do that, which property should I use? (I
could do a string parsing, but what a pain....)

Thanks

There is no property which would return you just the number, you will
have to do string parsing. One way of doing this could be the
following:

//assuming you have your element

var height = myDiv.style.height;
height = height.split("px")[0];
 
R

Randy Webb

(e-mail address removed) said the following on 7/5/2006 12:31 PM:
Thanks for the reply. I thought I could avoid having to do that... ok
then.

valueWithoutPX = parseInt(valueWithPX,10);

P.S. This is in the FAQ.

Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?
 
D

Dustin

Use the offsetWidth and offsetHeight properties. They are properties
of the element, not the elements style node.

var theDiv = document.getElementById("theDiv");
var width = theDiv.offsetWidth;
var height = theDiv.offsetHeight;

That works in IE6 and Firefox. I haven't tested it in other browsers.
 
M

Matt Kruse

Dustin said:
Use the offsetWidth and offsetHeight properties. They are properties
of the element, not the elements style node.
var theDiv = document.getElementById("theDiv");
var width = theDiv.offsetWidth;
var height = theDiv.offsetHeight;

This may not always work as expected, however.

offset* properties are always the outer dimensions - including padding and
borders.
However, in a page running in "strict mode" in IE or in standards-compliant
browsers, the specified style height of 176px does _not_ include padding and
borders. So the style height and the offsetHeight may actually be different,
and you need to use the one you actually want.
 
S

samuelberthelot

Thanks but it doesn't seem to work with FF :(
Randy said:
(e-mail address removed) said the following on 7/5/2006 12:31 PM:

valueWithoutPX = parseInt(valueWithPX,10);

P.S. This is in the FAQ.

Answer:It destroys the order of the conversation
Question: Why?
Answer: Top-Posting.
Question: Whats the most annoying thing on Usenet?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Temporarily at: http://members.aol.com/_ht_a/hikksnotathome/cljfaq/
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
 
M

Matt Kruse

Thanks but it doesn't seem to work with FF :(

The phrase "doesn't work" is meaningless.
See http://www.javascripttoolbox.com/clj/#getanswers

3. Give an accurate error message or description of what failed. Never use
the words "doesn't work". Saying that something "doesn't work" gives no
information about what was supposed to happen and specifically what happened
instead. Give more detail.

parseInt("150px",10)

will most certainly return 150 in FF, for example.
 
S

samuelberthelot

Sorry I was being lazy in the description of the error...
indeed it returns 150 so I had to convert it to a string and add 'px'.
All works fine now
thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top