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?
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.
If you want native JS to trigger click event without clicking then use the element id and click() method of JavaScript.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With