Div's top and left style properties yield NaN

R

Rick

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
 
R

RobG

Rick said:
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>
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top