Going Global: Global Events

You already used a few events in exercises, such as the onload event when you set the onload attribute of the <body> tag, or the onclick event when you set the onclick attribute of a <button> element.

HTML5 defines dozens of events that are triggered either as a result of user interactions, or as a result of state changes in the document’s lifecycle. You can attach event handler scripts to HTML elements. These handlers are fired when the specific event is triggered.

📜NOTE: You can easily distinguish event attributes from other ones, as their name always begins with the on prefix, such as onclick, onload, onblus, etc.

The scripts you set as the value of an event attribute can be any valid JavaScript, a sequence of statements as well as a single call.

The Listing below shows an example where you assign event handler code to two buttons using their onclick event attribute.

Get hands-on with 1400+ tech skills courses.