NachoRuba NachoRuba - 5 months ago 18
jQuery Question

Stop Onclick Execution when middle click is pressed - javascript / jquery

This is something like my code:

<li class="dropdown" id="dropdown1"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<b class="caret"></b></a>
<ul class="dropdown-menu">
<li id="li-1"><a href="#" onclick="Alert("li-1")">List1</a></li>
<li id="li-2"><a href="#" onclick="Alert("li-2")">List</a></li>
</ul>
</li>


but when middle click is pressed I want to stop execution of my onclick event.

Answer

Something like this disables the onclick whenever the middle button is clicked

$(document).mousedown(function(e) {
  if (e.which == 2) {
    e.preventDefault();
    alert("middle button");
    $('a').prop("onclick", null); // disable onclick
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<li class="dropdown" id="dropdown1"><a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown<b class="caret"></b></a>
  <ul class="dropdown-menu">
    <li id="li-1"><a href="#" onclick='alert("li-1")'>List1</a>
    </li>
    <li id="li-2"><a href="#" onclick='alert("li-2")'>List</a>
    </li>
  </ul>
</li>