0

I'm trying to disable mousewheel click on link, but I don't know why it doesn't work. Here is my code :

html :

<a class="disable_mousewheel_event" href="https://code.jquery.com/">Click</a>

javascript :

$(function() {
    $(".disable_mousewheel_event").on("mouseup", function(event) {
        if (event.which == 2) {
            event.preventDefault();
        }
    });
})
Dbi
  • 113
  • 1
  • 6
  • The tab opening does not happen on `mouseup`. It happens on `mousedown`. – blex Dec 13 '17 at 19:00
  • https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent 'mouseup' would be related to a button press. Checkout the wheel events. – Taplar Dec 13 '17 at 19:00
  • I already try "return false" and "mousedown". Doesn't work. – Dbi Dec 13 '17 at 19:11
  • Disabling a middle click is almost always the wrong approach. Are you also going to disable `ctrl+click`? You can't stop someone from opening the link in a new tab if they really want to. – zzzzBov Dec 13 '17 at 20:20
  • @zzzzBox He did actually the same and his example doesn't work, however you marked it as duplicated. Gj man :) – Andrii Pryimak Dec 13 '17 at 20:23

1 Answers1

-1

$(document).on("click", function(e) {
  if($(e.target).is("a[href]") && e.button === 1) {
    e.preventDefault();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="https://code.jquery.com/">Click</a>
Andrii Pryimak
  • 797
  • 2
  • 10
  • 33