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

140
Vistas
when do we need to put a parameter for the event handler

I am confused about events and event handlers, sometimes we call a function with an event handler and put a parameter for it and sometimes we do not. when do I put parameters and when is it necessay?

Here is an example:

<!DOCTYPE html>
<html>
<body>

<h2 onclick="showCoords(event)">Click this heading to get the x (horizontal) and y (vertical) coordinates of the mouse pointer, relative to the screen, 
when it is clicked.</h2>

<p><strong>Tip:</strong> Try to click different places in the heading.</p>

<p id="demo"></p>

<script>
function showCoords(event) {
  var x = event.screenX;
  var y = event.screenY;
  var coords = "X coords: " + x + ", Y coords: " + y
  document.getElementById("demo").innerHTML = coords;
}
</script>

</body>
</html>

where the showCoords has a parameter called event. This code is from W3Schools.

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

0

When you use an intrinsic event attribute (which have all sorts of issues) the variable event references the deprecated global event object.

So if you do this:

<button onclick="foo(event)">

function foo(event) {

}

You read window.event in the onclick function and pass it as an argument to the foo function.

While if you do this:

<button onclick="foo()">

function foo() {
    console.log(event)
}

Then you read window.event directly in the foo function.


When you use addEventListener (the modern approach to binding events that was introduced at the end of the last century):

function foo(event) {
    console.log(event)
}

document.querySelector('button').addEventListener('click', foo);

… then the event listener function (foo) will be passed a (non-deprecated) event object.

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