Step 4
The <script> tag
-
It will be difficult to read more than a few (JavaScript) statements for the
onclickattribute of thezzzbutton. Therefore, it is not advisable to write event handlers (JavaScript statements that are executed in response to an event) using attributes of button (or other HTML) elements. -
A better approach is to put all JavaScript statements (code) in a dedicated section, group the statements (about handling an event) in a function, and call that function in the
onclickevent. -
Add the following section to the end of the
<body>element (right before the closing</body>tag):
<script>
function handleOnClickEvent() {
window.alert('buzz!');
console.log('fizz!');
}
</script>
The
<script>element is used to include JavaScript in HTML.
- Update the
onclickattribute of thezzzbutton:
<button onclick="handleOnClickEvent()">zzz</button>
- Save the
index.htmlfile; refresh theindexpage in the browser. Then, click on thezzzbutton; it must work as before (producing alert window and console message).