3

I'm experimenting with the HTML5 contextmenu item in the latest version of Firefox (only browser that currently supports it). When I click on a menu item from the context menu how can I access the original element that was clicked when the menu was spawned?

The use case would be displaying a list of items and you want the user to perform an action on a particular item. Once the action is clicked from the menu you need a way to determine what element in the list was originally clicked on to spawn the menu.

Here is a JSFiddle (use Firefox to test)

berg
  • 614
  • 3
  • 9
  • 23

4 Answers4

2

Just save it somewhere like this.cte = arguments[0]; then retrieve it later document.getElementById('testitem').cte

http://jsfiddle.net/GALmp/4/

Musa
  • 96,336
  • 17
  • 118
  • 137
  • yeah that approach is the one I have working now but I was hoping for a better solution. – berg Aug 27 '12 at 04:43
1

I was in the same or very similar situation. I have a context menu bound to a <tr> in the header of a table. When the user clicks a certain item in the contextmenu, inside the click event handler I needed to determine which <th> element the mouse pointer was over when a <menuitem> element was clicked.

Solution: document.activeElement refers to exactly the element I need. Thanks to:

Community
  • 1
  • 1
cdaddr
  • 1,330
  • 10
  • 9
  • Thanks for this answer. It correctly answers the question asked, and help to solve a problem of mine. – Manngo Jul 30 '16 at 06:11
0

You can use the jquery contextMenu plug-in, which is browser independent. In particular, the following example: http://medialize.github.io/jQuery-contextMenu/demo/on-dom-element.html shows how to identify the triggering DOM element.

Leo
  • 1,869
  • 1
  • 13
  • 19
  • No you can’t. This is not the HTML5 Context Menu at all. Among other things it bypasses the existing context menu, so you lose the original items. – Manngo Jul 30 '16 at 06:05
-1

The this keyword works as in any event.

Paul S.
  • 64,864
  • 9
  • 122
  • 138
  • And it is completely useless as it points to the `` and not the item with the `contextmenu` attribute that opened it. – ThiefMaster Nov 26 '17 at 09:28