How to register events in JavaScript?

JavascriptWeb DevelopmentFront End Technology

Registering event handlers within the JavaScript code allows you to use the "this" statement. No need to pass a value to JavaScript functions, instead use "this" to manipulate properties.

Let’s see an example

Live Demo

<!DOCTYPE html>
         function display( ) {
            click.onclick = styleMe;
            uname.onclick = styleMe;
         function styleMe ( ) {
   = "#000000";
   = "#FFFFFF";
   <body onload="display()" >
      <div id="uname"> Hello Wordl!</div>
      <button id="click"> Click</button>
Updated on 19-Sep-2019 08:04:48