AFK
AFK

Reputation: 4501

Getting the Starting left position and top position of an element

In javascript I have a problem with a script I am writing. I need to be able to get the starting top and left positions of an html object like a table or an image that are defined in the CSS.

When I define the left(20px) and top(20px) offsets for the table I run the javascript console built into chrome and do parseInt(document.getElementById('component1').style.left) to see if it will give me the magnitude of the offset as a number. However, it returns NaN. I'm guessing because the browser isn't initializing these values(I thought setting it explicitly in the CSS, that the browser would)

Any ideas as to how I can get around this?

Upvotes: 0

Views: 918

Answers (1)

Nick Craver
Nick Craver

Reputation: 630449

Is jQuery an option? If so:

var offset = $("#component1").offset;
//use offset.left, offset.top for whatever you need

If not, you can do this by looping, see this answer.

Upvotes: 2

Related Questions