Step 3
The onclick
event
-
When we click on the
zzz
button, we want something to happen! -
Update the
button
tag and add the followingonclick
attribute:
<button onclick="window.alert('buzz!')">zzz</button>
HTML attributes provide additional information about HTML elements. They are always specified in the start tag, usually in name/value pairs like:
name="value"
.
- Save the
index.html
file; refresh theindex
page in the browser. Then, click on thezzz
button. You must see a pop-up alert window sayingbuzz!
Aside: The VSCode preview will not show this interaction. However, you can use the live server feature of the preview to open this page in the browser and observer interactivities.
The
window.alert('buzz!');
is a JavaScript statement. Thewindow
object represents an open window in a browser.
- Let's add another statement to
onclick
event of thezzz
button.
<button onclick="window.alert('buzz!'); console.log('fizz!')">
-
Save the
index.html
file; refresh theindex
page in the browser. -
In your browser, open "Developer Tools" (typically, you can do this by right-click and selecting "Inspect") and find the "Console" tab.
-
Now, click on the
zzz
button. In addition to a pop-up alert window with the messagebuzz!
, you must see the messagefizz!
printed on the console.
console.log()
provides access to the browser's debugging console.