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>
<html>
   <head>
      <script>
         function display( ) {
            click.onclick = styleMe;
            uname.onclick = styleMe;
         }
         function styleMe ( ) {
            this.style.backgroundColor = "#000000";
            this.style.color = "#FFFFFF";
         }
      </script>
   </head>
   <body onload="display()" >
      <div id="uname"> Hello Wordl!</div>
      <button id="click"> Click</button>
   </body>
</html>
raja
Published on 30-Jan-2018 11:34:56
Advertisements