Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

276
Vistas
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 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

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 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda