To add an event handler to an element, use the JavaScript addEventListener() method. You can try to run the following code to add event handler −
Example
<!DOCTYPE html> <html> <body> <p>Click the below button.</p> <button id = "btnid">Click me</button> <p id = "pid"></p> <script> document.getElementById("btnid").addEventListener("click", displayEvent); function displayEvent() { document.getElementById("pid").innerHTML = Date(); } </script> </body> </html>