Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery context menu - finding what element triggered it off

I am trying to write a context menu option for a page of mine. Basically a div is right-clicked, an options menu pops up which can be used to perform tasks.

My problem is trying to find the original element which triggered everything (ie the div that was right-clicked).

My jQuery code is more or lesS:

//this is what displays the context menu
$('.outfeedPosition').bind("contextmenu", function (e) {
    $('#contextMenu').css({
        top: e.pageY + 'px',
        left: e.pageX + 'px'
    }).show();

    //'this' is the element which was clicked by the user.
    alert($(this).attr('id'));

    return false;
});



//this is the contextMenu's button handler.
$('#ctxDelete').click(function () {
    alert('delete was clicked, but i dont know by which element - so I dont know which one to delete');
});


<div id="contextMenu">
    <ul>
        <li><a id="ctxInsert" href="#">Insert</a></li>
        <li><a id="ctxEdit" href="#">Edit</a></li>
        <li><a id="ctxDelete" href="#">Delete</a></li>
    </ul>
</div>

-- So - I can see what element created the event when the initial right-click happens. But not when the menu item is clicked.

I was working on fumbling something together by writing the element out to a hidden textbox when it is right-clicked, then reading it when one of the options is clicked, then removing it when the menu closes. Doesn't seem like the ideal approach though - and I feel like i'm missing something basic.

Hope you see what I am trying to do. I can post a more complete example on request.

like image 893
jb. Avatar asked Dec 13 '10 14:12

jb.


2 Answers

You could consider using the jQuery data storage methods.

In your context menu code you can put:

$('.outfeedPosition').bind("contextmenu", function (e) {
    $('#contextMenu').css({
        top: e.pageY + 'px',
        left: e.pageX + 'px'
    }).show();

    //Store the item that was clicked 
    $("#contextMenu").data('originalElement', this);

    return false;
});

Then when you want to reference the element that initiated the click, you can just do this:

$('#ctxDelete').click(function () {
    var originalElement = $("#contextMenu").data('originalElement');
    alert('delete was clicked by ' + originalElement.id );
});

And put originalElement in the jQuery function $() to access the jQuery goodness. It doesn't matter where you put the data, since any DOM element can have data associated to it. You can store anything - in the example code above, I store the HTMLElement raw (not jQueryified) but you can store that too if you want.

See here for a little example: http://www.jsfiddle.net/jonathon/sTJ6M/

like image 192
Jonathon Bolster Avatar answered Nov 15 '22 17:11

Jonathon Bolster


I add a hidden field and then find it based on the click, like this:

<div class="myItem">
    <div id="contextMenu">
        <ul>
            <li><a id="ctxInsert" href="#">Insert</a></li>
            <li><a id="ctxEdit" href="#">Edit</a></li>
            <li><a id="ctxDelete" href="#">Delete</a></li>
        </ul>
    </div>
    <input type="hidden" class="myID" value="1">
</div>

then with JQuery

$('#ctxDelete').click(function () {
    var id = $(this).closest('.myItem').find('.myID').val();   
    alert('delete was clicked, by element with ID = ' + id);
});
like image 24
Jason Avatar answered Nov 15 '22 17:11

Jason