<p>The problem I'm trying to solve is "What's at this position?"</p>

<p>It's fairly trivial to get the x/y position (offset) of a DIV, but what about the reverse? How do I get the id of a DIV (or any element) given an x/y position?</p>

[{'answer_id': 49001, 'author': 'Matt Mitchell', 'author_id': 364, 'author_profile': 'https://Stackoverflow.com/users/364', 'pm_score': 2, 'selected': False, 'text': '<p>Use a JQuery selector to filter the list of all DIVs for one that matches your position criteria?</p>\n'}, {'answer_id': 49009, 'author': 'Nick Retallack', 'author_id': 2653, 'author_profile': 'https://Stackoverflow.com/users/2653', 'pm_score': 2, 'selected': False, 'text': '<p>Create a mouse event listener, then trigger a mouse event at that location.  This should give you the entire stack of elements at that location.</p>\n\n<p>Or, look at the source of Firebug.</p>\n'}, {'answer_id': 49013, 'author': 'John Boker', 'author_id': 2847, 'author_profile': 'https://Stackoverflow.com/users/2847', 'pm_score': 0, 'selected': False, 'text': "<p>this might be a little too processor intensive but going over the whole list of div elements on a page, finding their positions and sizes then testing if they're under the mouse.  i don't think i'd want to do that to a browser though.</p>\n"}, {'answer_id': 49040, 'author': 'Rob Rolnick', 'author_id': 4798, 'author_profile': 'https://Stackoverflow.com/users/4798', 'pm_score': 1, 'selected': False, 'text': '<p>One option is to build an array of "div-dimension" objects. (Not to be confused with the divs themselves... IE7 perf is frustrating when you read dimensions off of object.)</p>\n\n<p>These objects consist of a pointer to the div, their dimensions (four points... say top, left, bottom, and right), and possibly a dirty bit. (Dirty bit is only really needed if the sizes change. </p>\n\n<p>You could then iterate through the array and check dimensions. It requires O(n) to do that on each mouse move. You might be able to do slightly better with a binary search style approach... maybe.</p>\n\n<p>If you do a binary search style approach, one way is to store 4 arrays. Each with a single point of the dimension, and then binary search on all four. O(4logn) = O(logn).</p>\n\n<p>I\'m not saying I recommend any of these, but they MIGHT work. </p>\n'}, {'answer_id': 49059, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>If all you have is the X and Y position, (and you can't track mouse movement like you mentioned) then you will have to traverse the DOM, looping through every DIV. For each DIV you will need to compare its X and Y coordinates against those you have. This is an expensive operation, but it is the only way. I suggest you might be better off rethinking your problem instead of coming up with a solution for it.</p>\n"}, {'answer_id': 49118, 'author': 'jonblock', 'author_id': 4647, 'author_profile': 'https://Stackoverflow.com/users/4647', 'pm_score': 0, 'selected': False, 'text': "<p>You might find it's more efficient to traverse the DOM tree once when the page is loaded, get all elements' positions and sizes, and store them in an array/hash/etc.  If you design the data structure well, you should be able to find an element at the given coordinates fairly quickly when you need it later.</p>\n\n<p>Consider how often you will need to detect an element, and compare that to how often the elements on the page will change.  You would be balancing the number of times you have to re-compute all the element locations (an expensive computation) against the number of times you'd actually use the computed information (relatively cheap, I hope).</p>\n"}, {'answer_id': 49418, 'author': 'Erlend Halvorsen', 'author_id': 1920, 'author_profile': 'https://Stackoverflow.com/users/1920', 'pm_score': 1, 'selected': False, 'text': '<p>I think what John is saying is that you can use document.createEvent() to simulate a mousemove at the location you want. If you capture that event, by adding an eventlistener to the body, you can look at the event.target and see what element was at that position. I\'m unsure as to what degree IE supports this method, maybe someone else knows?</p>\n\n<p><a href="http://developer.mozilla.org/en/DOM/document.createEvent" rel="nofollow noreferrer">http://developer.mozilla.org/en/DOM/document.createEvent</a></p>\n\n<p><strong>Update:</strong>\nHere\'s a jquery plugin that simulates events:</p>\n\n<p><a href="http://jquery-ui.googlecode.com/svn/trunk/tests/simulate/jquery.simulate.js" rel="nofollow noreferrer">http://jquery-ui.googlecode.com/svn/trunk/tests/simulate/jquery.simulate.js</a></p>\n'}, {'answer_id': 49936, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<pre><code>function getDivByXY(x,y) {\n   var alldivs = document.getElementsByTagName('div');\n\n   for(var d = 0; d &lt; alldivs.length; d++) {\n      if((alldivs[d].offsetLeft == x) &amp;&amp; (alldivs[d].offsetTop == y)) {\n         return alldivs[d];\n      }\n   }\n\n   return false;\n}\n</code></pre>\n"}, {'answer_id': 54595, 'author': 'rdworth', 'author_id': 5643, 'author_profile': 'https://Stackoverflow.com/users/5643', 'pm_score': 2, 'selected': False, 'text': '<p>Unfortunately, triggering a manufactured/simulated mouse event won\'t work, since when you dispatch it, you have to provide a target element. Since that element is the one you\'re trying to figure out, all you could do is dispatch it on the body, as if it had already bubbled.</p>\n\n<p>You really are left to do it on your own, that is manually walk through the elements you\'re interested in, and compare their position/size/zIndex to your x/y point and see if they overlap. Except in IE and more recently FF3, where you can use</p>\n\n<pre><code>var el = document.elementFromPoint(x, y);\n</code></pre>\n\n<p>See</p>\n\n<p><a href="http://developer.mozilla.org/En/DOM:document.elementFromPoint" rel="nofollow noreferrer">http://developer.mozilla.org/En/DOM:document.elementFromPoint</a></p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/ms536417(VS.85).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/ms536417(VS.85).aspx</a></p>\n'}]
['https://Stackoverflow.com/questions/48999', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5123/']