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

391
Vistas
how to add event listener to determine what the coordinates inside a div is clicked?

Suppose that I have div box that is 200px by 500px.

The objective is to add an event listener to determine the x and y coordinates inside of this div that is clicked.

How can that be down in React?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use getBoundingClientRect() to get the position of the element, plus MouseEvent.clientX and MouseEvent.clientY to get the click coordinate.

See demo:

function XYComponent() {
  const go = (e) => {
    const offsets = e.target.getBoundingClientRect();
    const y = e.clientY - offsets.top;
    const x = e.clientX - offsets.left;
    console.log(x, y);
  }
  return (
    <div>
      <div class="demo" onClick={go}>AAA</div>
      <div class="demo" onClick={go}>BBB</div>
    </div>
  )
}

ReactDOM.render(<XYComponent />, document.querySelector("#app"))
body {
  font-family: monospace;
}
.demo {
  border: 1px solid black;
  padding: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>

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