#Right Clickk not work in html + javascript

6 messages · Page 1 of 1 (latest)

jade fjord
#

As the title very well suggests, I have a checkbox in Javascript, or more like HTML. I have added an Event Listener for mouse down like so:

const checker = document.getElementById("clicker")
const maincheck = document.getElementById("maincheck")    
checker.addEventListener("mousedown", function(){
    if(event.button == 0){
        console.log("Ok")
        console.log("I am here")
        flareup.style.display = "block"
    }
    if(event.button == 1){
       maincheck.className = "animationclass"
       console.log("Is")
       }
  
})

When I left click it, it works perfectly fine - like a charm. But when I right click it, That's where problems arise. After left clicking it, when I right click it, nothing happens. I even added a check to print something in that instance and that corroborated my suspicion of nothing happening. I am not sure what's going on. I would appreciate help!

I don't know if this would help but fo context's sake here is the HTML:

<!DOCTYPE html>
<html>
<head>
<title>The Verification Game</title>
<link rel="stylesheet" href="./starting.css"></link>
</head>
<script src="https://kit.fontawesome.com/1f2023aaf1.js" crossorigin="anonymous"></script>
<body>
<div id="underneath">
<h2>Are you a human?</h2>
<input type="checkbox" id="realclicker"></input>
</div>
<div id="maincheck">
<h2>Are you a robot?</h2>
<input type="checkbox" id="clicker"></input>
</div>
<div id="incorrect">
<div id="inner">
<h1 class="fancytext">So you are a robot? You can't come here.</h1>
</div>
</div>
<script src="verification.js"></script>
</body>
#

@everyone

supple finch
#

If you want to use left-click just listen for 'click'.
To use 'right-click' listen for 'contextmenue' but orevent default behaviour like

el.addEventListener('contextmenu', function(ev) {
    ev.preventDefault();
    //do something with ev.target or whatever
    return false;
}, false);```
surreal cape
#

Hey, Saw the whole code and broke it down. Maybe the problem can be overcome if we prevent the context menu from appearing using 'contextmenu' event and it's 'preventDefault' method. Feel free to let me know if you need an optimized code.

frail meadow
#

You have the wrong button code. 2 is for the right button. You used 1, which is for the left button and 0, which isn't for any button. Your "event.button" happened to be equal to 0 only because it was undefined. That is what made the left click code work. Also, "event" is undefined in your code. Normally, the function takes the event object as a parameter, but your function does not take any parameters.

supple finch
#

I didn't know about event.button 😊 🙈
I think they can be differently assigned depending on the hardware 🤔