Page 115 - PortGPT_V2.1_C8_Flipbook
P. 115

8                                 Introduction to



                                                         Event Handling











                     Let’s surf
                         What is Event and Event Handling                Examples of Events
                         Onclick Event                                   Using Functions for Event Handling




                 The way  that JavaScript interacts  with  HTML  is  through  events that  happen  whenever a  user
                 or browser modifies a page. The actions of pressing any key/button, closing a window, resizing a
                 window, etc. are examples of events.



                        WHAT IS EVENT AND EVENT HANDLING

                 An event is defined as a change in an object's state. There are a number of events in HTML that show
                 when a user or browser performs a certain action. JavaScript responds to these events when JS code
                 is embedded in HTML and permits execution. The act of responding to events is known as event
                 handling. As a result, JavaScript uses event handlers to handle HTML events.



                        EXAMPLES OF EVENTS


                 There are various types of events that can occur. For example:
                 •  On a certain element, the user performs selection, clicking, or hovering of the cursor.

                 •  The user clicks a key on the keyboard.
                 •  Resizing or closing the browser window.

                 •  A web page completes loading.                                            Tech Fact

                 •  Submission of a form.                                       Event handlers are also referred
                 •  A video plays, is paused, or ended.                         to as event listeners.

                 •  An error takes place
                 You can attach an event handler to an event to respond to it. This is a section of code that executes
                 when the event occurs, often a JavaScript function that you write as a programmer. We refer to
                 defining such a section of code to execute in response to an event as "registering an event handler."






                                                                                      Introduction to Event Handling  113
   110   111   112   113   114   115   116   117   118   119   120