0

High all, I was wondering if it was possible to select an on page element via its coordinates?

reason being i'm trying to make an editable page where you can right click on highlighted elements, menu appears and then select from the options give. using the only method i could find that worked to select the element via hover it uses (event.target) which if having lots of parents combined with other code, it loops through and looses other data along the way. If i can find another method for highlighting the elements that would be of course better.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Phil Jackson
  • 10,238
  • 23
  • 96
  • 130
  • i've uploaded a demo to show. if you right click on the page, highlighting a paragraph but to the right, you should see the menu appear where it is suposed to, vanish then go in the top left corner. http://www.actwebdesigns.co.uk/test – Phil Jackson Dec 11 '09 at 08:40
  • 2
    See http://stackoverflow.com/questions/1569775/how-do-i-find-the-dom-node-that-is-at-a-given-x-y-position-hit-test and http://stackoverflow.com/questions/1471047/how-to-know-what-elements-are-in-current-mouse-position and http://stackoverflow.com/questions/48999/getting-div-id-based-on-x-y-position/54595#54595 – Crescent Fresh Dec 11 '09 at 08:41
  • Crescent Fresh you should also add that `elementFromPoint` behaves differently from browser to browser. Thus isn't really cross-browser-compatible – jitter Dec 11 '09 at 09:06
  • @jitter ...like most of the DOM API? kind of goes without saying ;) – Justin Johnson Dec 11 '09 at 10:18

1 Answers1

0

This is a terrible way to go about doing what you are trying to do, and I speak from experience: I inherited a calendar tool that figures out what day of the month you clicked on based on your mouse co-ordinates. The stupid thing certainly works, but it's a giant PITA to maintain/modify/add to in any way.

The much, much better way to go about it (unless you want a maintenance nightmare in your future) is to use the event object. If you hook up your events to the correct objects and use e.target (or, if you use jQUery, "this") you should be able to very easily route the correct actions based on the where the user clicked, without any coordinate nonsense.

machineghost
  • 33,529
  • 30
  • 159
  • 234