Friday, July 3, 2009

How To Find Position Of An HTML element

function GetPos(Cord,Obj) {
var curPos = 0;
try {
switch(Cord)
{
case 'X':
case 'Y':
if (Obj.offsetParent)
{
while (1)
{
if(Cord == 'X'){
curPos += Obj.offsetLeft;
}else{
curPos += Obj.offsetTop;
}
if (!Obj.offsetParent) break; Obj = Obj.offsetParent;
}
}
else
{
if(Cord == 'X'){
if (Obj.x)curPos += Obj.x;
}
else{
if(Obj.y)curPos += Obj.y;
}
}
break;
}
}
catch (e) {
document.write('An exception occurred.');
}
finally {
return curPos;
}
}

function callfn(Cord, func, obj) {

return func(Cord, obj);
}

//way of Calling Function
"alert(callfn('X',GetPos,document.getElementById('elementId')))" // X co-ordinates
"alert(callfn('Y',GetPos,document.getElementById('elementId')))" // Y co-ordinates

No comments: