views:

276

answers:

2

How can set the offset using JavaScript ?

+1  A: 

You set the left and top CSS values.

With jQuery

$("#myEl").css({"position":"absolute","left":"100px","top":"100px"});

Straight Javascript

document.getElementById("myEl").style.position = "absolute";
document.getElementById("myEl").style.left = "100px";
document.getElementById("myEl").style.top = "100px";
Jonathan Sampson
He said JavaScript, not jQuery.
drlouie - louierd
You may want to read the tags again for this question, Louie. And besides, that's why I provided both Javascript and jQuery.
Jonathan Sampson
A: 

You could also add a class, e.g.

Jquery: $("p:last").addClass("selected");

Javascript: Someone has posted a JS function here - http://bytes.com/topic/javascript/answers/542128-can-javascript-add-new-style-class

You would then need to make sure that 'selected' was defined in your style sheet. The relevant CSS properties are:

top left margin padding

You probably want a combination of margin (offset outside element) and padding (offset inside element).

Aaron Newton