Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering onclick event using middle click

I am using the onclick event of a hashed link to open a <div> as a pop up. But the middle click does not trigger the onclick event but only takes the href attribute value of the link and loads the URL in a new page. How can I use middle click to open the <div> as a popup?

like image 782
Sadesh Kumar N Avatar asked Nov 25 '09 09:11

Sadesh Kumar N


People also ask

How do you trigger an event on click?

If you just need to trigger a click event, you can omit the line that begins with for( . @Parag: Read it again. The loop is to click the same link 50 times, which is what it does.

How do I trigger a click event without clicking?

If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.

What is Auxclick?

The auxclick event is fired at an Element when a non-primary pointing device button (any mouse button other than the primary—usually leftmost—button) has been pressed and released both within the same element.

Can a div have a click event?

To add a click event listener on div tag using JavaScript, we can call the addEventListener method on the selected div. to add a div with a class. Then we write: const div = document.


1 Answers

EDIT

This answer has been deprecated and doesn't work on Chrome. You will most probably end up using the auxclick event, but please refer to other answers below.

/EDIT


beggs' answer is correct, but it sounds like you want to prevent the default action of the middle click. In which case, include the following

$("#foo").on('click', function(e) {    if (e.which == 2) {       e.preventDefault();       alert("middle button");     } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a id="foo" href="http://example.com">middle click me</a>

preventDefault() will stop the default action of the event.

like image 180
Gausie Avatar answered Oct 05 '22 08:10

Gausie