Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can i get the height or width of an element?

Status
Not open for further replies.

robi2

Programmer
Jan 19, 2003
62
IL
Hi, i can't seem to figure out how i get get the height or width of an object if i didn't set it, i just wrote text.

I mean that i have a div, and i have written about 5 sentences into it, and its height and width are based on the text, so how can i get these values?
 
Try this...

<div id=&quot;myDiv&quot;>some text</div>


divHeight = document.getElementById(&quot;myDiv&quot;).height

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
errr no good..

it just returns undefined
 
divNode = document.getElementById(&quot;myDiv&quot;).height

for (x=0; x<divNode.attributes.length; x++){
mess += &quot;\n&quot; + divNode.attibutes[x].name + &quot; - &quot; + divNode.attibutes[x].value
}
alert(mess)


see what info is there...

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
the whole attributes thing doesn't work...

it just tells me object expected..
i've tried searching for this, also i removed the &quot;.name&quot; from the divNode.

it goes into the loop but then it just says object expected.

this is what i tried:
attributes[x].name
attribute[x].name
attributes[x].attribute.name

none work... whats the correct syntax for the attributes?
 
<div id=&quot;myDiv&quot; style=&quot;height:20px;border: 1px solid blue;&quot;>some text</div>

<script>
divNode = document.getElementById(&quot;myDiv&quot;)

alert(divNode.style.height)

divNode.style.height = 100
</script>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook
 
This is what im doing:

<div id=&quot;myDiv&quot;>some text
some text
some text
some text
some text
some text</div>

Now the div will automaticly be the required height to fit the text, and i want to know what is that height, i do not set it, it automatically sets itself.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top