• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

219
Views
How to listen for mouse clicks outside of button element?

I want to be able to just listen for a mouse click anywhere on the tab, and then be able to execute a function. Specifically, I want to listen for button 5 (1st button on mouse side buttons). Thanks in advance

about 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Before your question, I never realized you could listen for events outside left and right-click. I'm not too certain of what you're asking.

index.html

<div class="container">
  <button id="button" oncontextmenu="event.preventDefault();">Click here.</button>
  <div id='modal'>
    <span id='display'>Mouse Button Type</span>
  </div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

main.js

let button = document.querySelector('#button');
let log = document.querySelector('#display');

// display module
$(button).click('mouseup',function () {
  $("#modal").css('display','block');
    logMouseButton();
})

// bind mouse event to body
$('body').click(function (e) {
  // You can put whatever here  
  if ($(e.target).is($("#modal"))) {
        $("#modal").css('display','none');
    }
})

function logMouseButton(e) {
  if (typeof e === 'object') {
    switch (e.button) {
      case 0:
        log.textContent = 'Left button clicked.';
        break;
      case 1:
        log.textContent = 'Middle button clicked.';
        break;
      case 2:
        log.textContent = 'Right button clicked.';
        break;
      default:
        log.textContent = `Unknown button code: ${e.button}`;
    }
  }
}


style.css

html {
  margin: 0;
  padding: 0;
}
body {
  margin: 0;
  padding: 0;
  background: url('https://wallpapercave.com/wp/wp6242088.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
  z-index: 0;
}
.container { 
  position: absolute;
  z-index: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  #button {
    margin: 0 auto;
    width: 25%;
    padding: 1rem 2rem;
    font-weight: bold;
    font-size: 1.5rem;
    font-family: "Open Sans", sans-serif;
    color: black;
    border-radius: 0.5rem;
    background-color: rgba(255,255,255,0.4);
    backdrop-filter: blur(10px);
    border: none;
  }
  span {
    margin: 2rem auto 0 auto;
    width: 25%;
    height: 40px;
    border-radius: 0.5rem;
    background-color: rgba(255,255,255,0.4);
    backdrop-filter: blur(10px);
    border: none;
    padding: 1rem 2rem;
    color: black;
    font-family: 'Open Sans', sans-serif;
    font-weight: bold;
    font-size: 1rem;
    display: flex;
    align-content: center;
    justify-content: center;
  }
  #modal {
    display: flex;
    justify-content: center;
    align-content: center;
    position: absolute;
    z-index: 3;
    background-color: rgba(255,255,255,0.4);
    backdrop-filter: blur(10px);
    width: 100%;
    height: 100vh;
    display: none;
  }
}

CodePen: Link to CodePen Reference: MouseEvent.button

about 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error