MDN wants to learn about developers like you:

Esta traducción está incompleta. Por favor, ayuda a traducir este artículo del inglés.


La propiedad onclick devuelve el manejador del evento click del elemento actual.

Note: Cuando uses el evento click para ejecutar algo, considera agregar la misma acción al evento keydown, para permitirle el uso a personas que no usan mouse o pantalla táctil.


element.onclick = functionRef;

where functionRef is a function - often a name of a function declared elsewhere or a function expression. See "JavaScript Guide:Functions" for details.

The single argument passed to the specified event handler function is a MouseEvent object. Within the handler, this will be the element upon which the event was triggered.


<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8" />
    <title>onclick event example</title>
      function initElement() {
        var p = document.getElementById("foo");
        // NOTE: showAlert(); or showAlert(param); will NOT work here.
        // Must be a reference to a function name, not a function call.
        p.onclick = showAlert;

      function showAlert(event) {
        alert("onclick Event detected!");
      #foo {
        border: solid blue 2px;
  <body onload="initElement();">
    <span id="foo">My Event Element</span>
    <p>click on the above element.</p>

Or you can use an anonymous function, like this:

p.onclick = function(event) { alert("moot!"); };


The click event is raised when the user clicks on an element. The click event will occur after the mousedown and mouseup events.

Only one click handler can be assigned to an object at a time with this property. You may be inclined to use the EventTarget.addEventListener() method instead, since it is more flexible and part of the DOM Events specification.


Especificación Estatus Comentario
WHATWG HTML Living Standard
The definition of 'onclick' in that specification.
Living Standard  

Compatibilidad de Navegadores

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Soporte básico (Yes) ? ? ? ?
Feature Android Android Webview Chrome for Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile
Soporte básico ? ? ? ? ? ? ? ?

Etiquetas y colaboradores del documento

 Colaboradores en esta página: gama
 Última actualización por: gama,