I’m trying to position a div with the written method findPos() but I don’t know how to start out. Google did not help me so much.
findPos:
function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
do {
curleft += obj.offsetLeft;
curtop += obj.offsetTop;
} while (obj = obj.offsetParent);
return [curleft, curtop];
}
}
My code:
fname.onfocus = function() {
tooltip("Insert name", fname);
}
function tooltip(text, form) {
var div = document.createElement("div");
div.className = "tool";
var info = document.createTextNode(text);
div.appendChild(info);
form.parentNode.insertBefore(div, form.nextSibling);
}
Any ideas?
Not sure if I understand what are you trying to achieve, but I will try.
findPos()is a function that takes an element inserted in the DOM and returns its left and top position. The element has to be inserted in the DOM and you cannot use this function to set position of a created element.To set a position use for example:
For more information have a look here: https://developer.mozilla.org/en/CSS/position