LouieV LouieV - 1 year ago 63
jQuery Question

How to get the position of an element using .offset()

I have a table with a menu hidden in a cell that that I expand using

slideToggle()
. This menu has a few
<img>
that
onclick=
call a function and passed the element and a number. Now I'm trying to get the position of the element that made the call so I can dynamically create an overlay
<div>
. The problem is that I am not getting the position values and I don't know why. HERE is a small example of what I am trying to do. Can anyone help?

Answer Source

I believe the issue is your use of $(this). You are passing the element that was clicked into your function so you need to use that instead. I have also updated your alert to explicitly output the left offset as offset would just return an object. You could instead use console.log to be able to inspect the returned object (in a browser like chrome):

window.somefunc = function(el,id){
    var pos = $(el).offset();
    alert(pos.left);
}

I have updated your JSFiddle here: http://jsfiddle.net/gRfnb/22/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download