Rajat Gupta
Rajat Gupta

Reputation: 26597

Positioning an element using values from Javascript variables

How to position an element for e.g. a div tag by using values for x & y co-ordinates from Javascript variables ?

Upvotes: 2

Views: 6101

Answers (2)

jfriend00
jfriend00

Reputation: 707436

If you're trying to position the object in absolute x,y coordinates on the page, just set the object to position: absolute and then set the values of top and left to your x and y.

HTML:

<div id="square" style="background-color: #777; height: 100px; width: 100px;"></div>

Code:

var s = document.getElementById("square");
s.style.position = "absolute";
var x = 100, y = 200;
s.style.left = x + "px";
s.style.top = y + "px";

You can see a working code example here: http://jsfiddle.net/jfriend00/hhpDQ/.

Upvotes: 3

mowwwalker
mowwwalker

Reputation: 17362

I'm not sure you can set the coordinates, but you could set the margin. Something like

$("#divid").css("margin-left" : "200px" , "margin-up" : "200px");

Upvotes: 0

Related Questions