Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 116 - 2625 PortGPt V2.1
P. 116

    ONCLICK EVENT

                      Let’s see an example:
                      The simplest and most widely used is the click event which occurs on the click of a button. In this
                      example, when the button is clicked, the event onclick is triggered.

                      <HTML> <BODY bgcolor="pink">

                      <input type="button" value="Click Me" onclick="alert('Hi You clicked a
                      button')">
                      </body> </html>

                      Here,

                                               <button>            onclick event         Your code
                                              gets clicked          happens                runs!

                      Using an HTML attribute with the name of the event handler allows you to add an event handler
                      to an event connected to an HTML element. A message box (called alert box) appears in this

                      instance when the button is clicked. In other words, we attach a click event listener to the button
                      element.
                      Example 2:

                      <html>

                      <body>
                      <h2>Event Handling in JavaScript</h2>
                      <button  onclick="this.innerHTML=Date()">Click  to  find  date  and  time</
                      button>
                      </body>

                      </html>

                      114   Premium Edition-VIII

| 111 112 113 114 115 116 117 118 119 120 121 |