sarmiena
May 7th 2008, 3:39 pm
The following code is supposed to get the bottom right position of a textbox and set the top left position of a span to those coordinates. It works great with ie7, but not with firefox... any clues?
window.onload = function(){
computeBottomRight();
}
window.onresize = function(){
computeBottomRight();
}
function computeBottomRight(){
//Get the "anchor" element and store it in variable
var element = document.getElementById("TextBox1");
//get element that moves with the anchor
var movableElement = document.getElementById("span1")
//get the length and width of anchor element and store them
var elementWidth = element["offsetWidth"];
var elementHeight = element["offsetHeight"];
//Compute top right of anchor element and add width & height
//to get bottom right position
var x = getAbsPos(element,"Left") + elementWidth;
var y = getAbsPos(element,"Top") + elementHeight;
//Display coords of bottom right in textbox
document.getElementById("TextBox1").innerText = "(" + x + "," + y + ")";
//move the movable element
movableElement.style.pixelLeft = x;
movableElement.style.pixelTop = y;
}
function getAbsPos(element, side){
var position = 0;
while (element != null) {
position += element["offset" + side];
element = element.offsetParent;
}
return position;
}
window.onload = function(){
computeBottomRight();
}
window.onresize = function(){
computeBottomRight();
}
function computeBottomRight(){
//Get the "anchor" element and store it in variable
var element = document.getElementById("TextBox1");
//get element that moves with the anchor
var movableElement = document.getElementById("span1")
//get the length and width of anchor element and store them
var elementWidth = element["offsetWidth"];
var elementHeight = element["offsetHeight"];
//Compute top right of anchor element and add width & height
//to get bottom right position
var x = getAbsPos(element,"Left") + elementWidth;
var y = getAbsPos(element,"Top") + elementHeight;
//Display coords of bottom right in textbox
document.getElementById("TextBox1").innerText = "(" + x + "," + y + ")";
//move the movable element
movableElement.style.pixelLeft = x;
movableElement.style.pixelTop = y;
}
function getAbsPos(element, side){
var position = 0;
while (element != null) {
position += element["offset" + side];
element = element.offsetParent;
}
return position;
}