Jquery - right click not working

I have the following test code:

<script language="JavaScript" src="..\Generic\JAVASCRIPT\jQuery-min.js" type="text/javascript"></script>
<script language="JavaScript" src="..\Generic\JAVASCRIPT\jQuery-ui-min.js" type="text/javascript"></script>
<script language="JavaScript">
$(document).ready(function()
{
    $('#SG1').click(function(event) {
    switch (event.which) {
        case 1:
            alert('Left Mouse button pressed.');
            break;
        case 2:
            alert('Middle Mouse button pressed.');
            break;
        case 3:
            alert('Right Mouse button pressed.');
            break;
        default:
            alert('You have a strange Mouse!');
    }
})  
})

</script>

<p class="submit">
    <input id="SG1" type="submit" name="submit" value="SG1">
</p>

      

If I LEFT click it works fine (event 1).

If I MIDDLE click it works fine (event 2).

If I press CORRECTLY, nothing happens (well, I get the normal menu, but no warning)

If I change the action from click

to mousedown

, it works as expected. Why doesn't this work with click

? Am I missing something?

+3


source to share


3 answers


The problem with your code is that the right click raises an event contextmenu

. If you want to disable this check out this fiddle: http://jsfiddle.net/4gk7zv9z/



In this fiddle, when the contextmenu event is called, the JS code does not allow the context menu to be displayed and allows the programmer to do what he likes.

0


source


Try disabling the context menu:

$ ("# SG1"). bind ("contextmenu", function () {return false;});



Then it should work.

+2


source


Use mousedown

FIDDLE

$(document).ready(function()
{
    $('#SG1').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left Mouse button pressed.');
            break;
        case 2:
            alert('Middle Mouse button pressed.');
            break;
        case 3:
            alert('Right Mouse button pressed.');
            break;
        default:
            alert('You have a strange Mouse!');
    }
})  
})

      

+1


source







All Articles