HTML DOM addEventListener() method Element object Example is Add a click event to the element. When the user clicks the button, on the element with id="demo", output "Hello World": document.getElementById("myBtn").addEventListener("click", function(){ document.getElementById("demo").innerHTML = "Hello World"; }); Try it » Definition and Usage The addEventListener() method is used to add an event handler to a specified element. Tip: Use removeEventListener() The method is used to remove the event handler added by the addEventListener() method. Browser support The numbers in the table indicate the first browser version that supports this method. Method addEventListener() 1.0 9.0 1.0 1.0 7.0 Note: Internet Explorer 8 and earlier IE versions do not support the addEventListener() method, nor do Opera 7.0 and earlier versions of Opera. However, for these browsers that do not support this function, you can use the attachEvent() method to add an event handler (see "More Examples" for the cross-browser solution). Syntax element .addEventListener( event , function , useCapture ) Parameter value Parameters Description event Required. A string specifying the event name. **注意:** 不要使用 "on" 前缀。 例如,使用 "click" ,而不是使用 "onclick"。 Tip: For all HTML DOM events, you can view our complete [HTML DOM Event Object Reference Manual](/jsref/dom-obj-event.html). | function | Required. The function to be executed when the event is triggered. 当事件对象会作为第一个参数传入函数。 事件对象的类型取决于特定的事件。例如, "click" 事件属于 MouseEvent(鼠标事件) 对象。 | | useCapture | Optional. A Boolean value specifying whether the event is executed during the capturing or bubbling phase. 可能值: true - The event handler is executed during the capturing phase false - false - Default. The event handler is executed during the bubbling phase | Technical Details DOM version: DOM Level 2 Events Return value: No return value Record: In Firefox 6 and Opera 11.60 the useCapture parameter is optional. (It has always been optional in Chrome, IE, and Safari). More examples Example You can reference an external function by its function name. This example demonstrates how to execute a function when the user clicks the element: document.getElementById("myBtn").addEventListener("click", myFunction); function myFunction() { document.getElementById("demo").innerHTML = "Hello World"; } Try it » Example You can add many events to the document, and the added events will not overwrite existing events. This example demonstrates how to add two click events to theelement: document.getElementById("myBtn").addEventListener("click", myFunction); document.getElementById("myBtn").addEventListener("click", someOtherFunction); Try it » Example You can add different types of events to the same element. This example demonstrates how to add multiple events to the same element: document.getElementById("myBtn").addEventListener("mouseover", myFunction); document.getElementById("myBtn").addEventListener("click", someOtherFunction); document.getElementById("myBtn").addEventListener("mouseout", someOtherFunction); Try it » Example When passing parameter values, use an "anonymous function" to call a function with parameters: document.getElementById("myBtn").addEventListener("click", function() { myFunction(p1, p2); }); Try it » Example Modify The background of the element: document.getElementById("myBtn").addEventListener("click", function(){ this.style.backgroundColor = "red"; }); Try it » Example Use the optional parameter useCapture to demonstrate the difference between the bubbling and capturing phases: document.getElementById("myDiv").addEventListener("click", myFunction, true); Try it » Example Use the removeEventListener() method to remove the event handler added by the addEventListener() method: // Add an event handler document.getElementById("myDIV").addEventListener("mousemove", myFunction); // Remove an event handler document.getElementById("myDIV").removeEventListener("mousemove", myFunction); Try it » Example If the browser does not support the addEventListener() method, you can use the attachEvent() method instead. The following example demonstrates a cross-browser solution: var x = document.getElementById("myBtn"); if (x.addEventListener) { // All major browsers, except IE 8 and earlier IE versions x.addEventListener("click", myFunction); } else if (x.attachEvent) { // IE 8 and earlier IE versions x.attachEvent("onclick", myFunction); } Try it » Related pages JavaScript tutorial: HTML DOM EventListener JavaScript Reference Manual: document.addEventListener() Element object