jQuery context menu get clicked item

18,178

Solution 1

not familiar with the plugin, but from the looks of it you should be able to write:

$("#" + t.id).html();

but in the case of most jQuery plugins you should be able to do this:

$(this).html();

from within the context of the 'copy': function(t) { and 'delete': function(t) {


$('#contacts tbody tr').contextMenu('myMenu1', {
    bindings: {
        'open': function(t) { ShowAction(t, "Open"); },
        'email': function(t) { ShowAction(t, "Email"); },
        'save': function(t) { ShowAction(t, "Save"); },
        'delete': function(t) { ShowAction(t, "Delete"); }
    }
});

function ShowAction(t, a) {
    alert('Trigger was ' + t.id + '\nAction was ' + a + "\nHtml is " + $(t).html());
}

Here's a working example: http://jsfiddle.net/dNUgg/

I'm guessing your <tr> tags do not have an id attribute


Even when the <tr> does not have an ID this still works: http://jsfiddle.net/dNUgg/1/


 alert('content is ' + $(t).text() + '\nAction was Delete');

updated your jsfiddle: http://jsfiddle.net/gqhRV/1/

you were doing $(t.target).text() when you should be doing $(t).text()

Solution 2

I think is this what you want:

<script type="text/javascript"> 
$(function(){
$.contextMenu({
    selector: '.flexme1 tbody tr', 
    callback: function(key, options) {
        alert("Clicked on " + key + " on element " + options.$trigger.attr('id').substr(3));            
    },
    items: {
        "edit": {name: "Edit", icon: "edit"},
        "cut": {name: "Cut", icon: "cut"},
        "copy": {name: "Copy", icon: "copy"},
        "paste": {name: "Paste", icon: "paste"},
        "delete": {name: "Delete", icon: "delete"},
        "sep1": "---------",
        "quit": {name: "Quit", icon: "quit"}
    }
});

$('.flexme1 tbody tr').on('click', function(e){
    console.log('clicked', this);
})
});
</script>

It's integrated with the Flexigrid... works fine for me...

And obviously i have some extra options.

Share:
18,178
DarkLeafyGreen
Author by

DarkLeafyGreen

Tackling Complexity in the Heart of Software

Updated on June 04, 2022

Comments

  • DarkLeafyGreen
    DarkLeafyGreen almost 2 years

    I am using jQuery context menu plugin by Chris Domigan to appy a context menu. This is how I do it:

    $('#contacts tbody tr').contextMenu('myMenu1', {
        bindings: {
            'copy': function(t) {
                 alert('Trigger was '+t.id+'\nAction was Copy');
             },
    
            'delete': function(t) {
                 alert('Trigger was '+t.id+'\nAction was Delete');
            }
        },             
    });
    

    My question is, how do I get the content of the clicked tr item? I tried with

    $(t.target).html()
    

    but it returns null. Any idea?

    EDIT: here is the example http://jsfiddle.net/gqhRV/