469,929 Members | 1,723 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

Div's top and left style properties yield NaN

I give up. I've been banging my head on the keyboard for the last
half-hour over this one. Can someone please point out what the heck I'm
doing wrong? I'm trying to move a block of text on screen. I have a div
element like this:

<div name="thetext" id="thetext" class="mtext">
blah blah blah
</div>

In the script to move the div, I try to retrieve the div's current
position. I do it this way:

obj = document.getElementById('thetext');
topPos = parseInt(obj.style.top);
leftPos = parseInt(obj.style.left);
I keep getting "NaN" for both topPos and leftPos. Can anyone tell me
why?

--Rick

Sep 28 '05 #1
1 6953
Rick wrote:
I give up. I've been banging my head on the keyboard for the last
half-hour over this one. Can someone please point out what the heck I'm
doing wrong? I'm trying to move a block of text on screen. I have a div
element like this:

<div name="thetext" id="thetext" class="mtext">
blah blah blah
</div>

In the script to move the div, I try to retrieve the div's current
position. I do it this way:

obj = document.getElementById('thetext');
topPos = parseInt(obj.style.top);
leftPos = parseInt(obj.style.left);
I keep getting "NaN" for both topPos and leftPos. Can anyone tell me
why?


Probably because they are undefined and parseInt() thinks 'undefined' is
not a number. Did you try -- alert(obj.style.left) -- ?

obj.style is a reference to obj's style object. You are accessing its
properties, but the code you posted does not set any of them, it just
sets the className to 'mtext'. If you want a solution:

<URL:http://www.quirksmode.org/js/findpos.html>


--
Rob
Sep 28 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by mike | last post: by
reply views Thread by smr78 | last post: by
4 posts views Thread by jobs | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.