Get the id of the clicked element that opened the context menu using jQuery contextMenu Plugin

I am using jQuery contextMenu from Rodney Rem in SVG graphics. It works great for basic use.

But I need to get the identifier (or any other property) of the SVG-Element that called the context menu in order to use it in the list of context menu items in order to get dynamic element names.

I am working with a demonstration of the Simple Context Menu and now I want to replace these static menu items with dynamic ones, depending on the identifier of the SVG element that was clicked.

+5
source share
3 answers

: http://medialize.github.com/jQuery-contextMenu/demo/dynamic-create.html
:

$(function(){
    $.contextMenu({
        selector: 'my-selector-here', 
        build: function($trigger, e) {
            // this callback is executed every time the menu is to be shown
            // its results are destroyed every time the menu is hidden
            // e is the original contextmenu event, containing e.pageX and e.pageY (amongst other data)
            // $trigger is the element that was rightclicked on - get its id here
            var id = $trigger.getTheIDSomehow()
            // build the menu items
            if (id == 1) {
              menuItems = {...}
            else if (id == 2)
              menuItems = {...}
            return {
                callback: function(key, options) {
                    // this is called when one of the contextmenu options is clicked
                },
                items: menuItems
            };
        }
    });
});
+10

, :

$.contextMenu({
            selector: 'tr',
            callback: function (key, options) {
                var m = "clicked: " + key;
                if (key == "Clone") 
               {
                    Your_Function($(this).attr('id'));
               }

            },
            items: {
                "Clone": { name: "Clone" },
                }
            }); 
+6

when i use static menus i get id like this:

...
callback: function (key, options) {
    id = options.$trigger.attr("id");
    ...
},
...

Maybe $ trigger.attr ("id") may work for you.

+4
source

All Articles