Muhammed Almaz Muhammed Almaz - 3 months ago 28
jQuery Question

Contextmenu right click event


http://jsbin.com/iGaHAtu/2/edit?html,css,js,output


Look at this link i want to get clicked table doom element . How can i do it ?
I am going to try this code but doesn't worked.

For Example i want to column text when i clicked right and openned contextmenu.. Can anyone alert the inner column ?

$("body").on("contextmenu", "table tr", function(e) {
$contextMenu.css({
display: "block",
left: e.pageX,
top: e.pageY
});
console.log($(this));
return false;
});

Answer

Use $(e.target) for jquery object representing clicked element. Otherwise just use e.target for plain old javascript dom element.

In your case, to alert the column text, try this code:

$(function() {

  var $contextMenu = $("#contextMenu");

  $("body").on("contextmenu", "table tr", function(e) {
    $contextMenu.css({
      display: "block",
      left: e.pageX,
      top: e.pageY
    });
    alert($(e.target).text());
    return false;
  });

  $contextMenu.on("click", "a", function() {
     $contextMenu.hide();
  });

});

More on event.target here: https://developer.mozilla.org/en-US/docs/Web/API/Event/target