How to get the co-ordinates of an element after onclick event ? I need to place an overlay just below an icon that was clicked.
Is there any such built in function provided by Javascript rather than manually writing a function for that ?
How to get the co-ordinates of an element after onclick event ? I need to place an overlay just below an icon that was clicked.
Is there any such built in function provided by Javascript rather than manually writing a function for that ?
In YUI, I use the getXY() function that returns the absolute page position of any object in the DOM. The source for that function in YUI2 is here: http://developer.yahoo.com/yui/docs/Dom.js.html.
In jQuery, it looks like you would use the .offset() method of a jQuery object: http://api.jquery.com/offset/.
You can either use one of those or look at their implementation and see how they work to code your own.
Try this one:
function getGlobalPos(el) {
var p = {x:0,y:0};
while (el) {
p.x += el.offsetLeft;
p.y += el.offsetTop;
el = el.offsetParent;
}
return p;
}