1

I'm using event.preventDefault() for all elements on the page with Jquery like this:

$('*').on('click', function(e) {
e.stopPropagation();
e.preventDefault();
});

Now I would like to trigger a click event using the the context menu on right click. So basically I would right click the mouse and a context menu would open and from that menu I would choose to trigger the click. In Jquery this is simple I would do this in my function related to the context menu:

$(this).click();

The this part would get the selector and the click part would trigger the click, of course with return true to lift the preventDefault. The problem is that I can't use Jquery in the latter, I have to use plain Javascript. What is the equivalent of $(this).click(); in plain Javascript? (without attaching click handlers on the elements themselves that is)

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Youss
  • 4,196
  • 12
  • 55
  • 109
  • @meagar I can't use `document.getElementById` as in the "duplicate" because I don't know what I'm clicking on upfront...That is basically the point of my question – Youss Oct 05 '14 at 20:57
  • @meagar Care to explain yourself...? – Youss Oct 05 '14 at 21:01
  • 1
    You can use document.addEventListener, I wrote a small demo here on [jsfiddle](http://jsfiddle.net/0gn0r2ej/1/) – ClemSndr Oct 05 '14 at 21:02
  • 1
    And as you said I agree this is not the exact same thing as the "duplicate", trigger must have a target and in this case we want an event listener – ClemSndr Oct 05 '14 at 21:04
  • @ClemSndr I don't get it..How should I use this when programmatically clicking on various elements? – Youss Oct 05 '14 at 21:09
  • 1
    The code I gave you is a replacement for `$('*').on('click', function(e) {`, then you have to wrap you code inside it and use e.target.id to act on the element wich has been clicked – ClemSndr Oct 05 '14 at 21:12
  • @ClemSndr Ah I get it thank you very much, and thanks again for elaborating on it:) – Youss Oct 05 '14 at 21:16

0 Answers0