Page 116 - PortGPT_V2.1_C8_Flipbook
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

