If you have been following this course from the beginning, you have understood that:
The purpose of JavaScript is to manage the interactions between the user and the page (HTML/CSS). These interactions are triggered by an action (usually from the the user). For example:
These actions are called events in JavaScript. An event is composed of of two parts:
For example, if an alert is displayed when a button is clicked:
The event mechanisms are very powerful and allow to perform an infinite number of sometimes very complex interactions. Today, the majority of online video games (like slither.io for example) are written in JavaScript.
We will start with the simplest syntax: writing the event directly in the attribute of an HTML tag:
<balise cause='consequence'>
The example below triggers an alert when the user clicks on the
paragraph text (<p></p>
).
<p onclick="alert('You have triggered an event by clicking.')">
CLick here to display an alert.
</p>
Here is the result:
In the above example, there is a paragraph in <p></p>
tags. These
tags have an onclick
attribute which indicates that an event will be triggered if the user
clicks in this paragraph.
The onclick
attribute contains JavaScript code, here an alert.
this
is a JavaScript keyword that designates the current element. In the example
above, this
would represent the <p></p>
paragraph in which the onclick
attribute is located.
this
allows to modify the content or the properties of this element. The example below
below allows you to change the text of the paragraph when you click on it:
<p onclick="this.innerText='Text has changed.' ">
Click here to change the text.
</p>
Let's analyze the code of the onclick
attribute:
this.innerText='Text has changed.'
this
is the current element (here the <p></p>
tag in which the event is located)innerText
is a keyword that describes the text contained in the tag, the inner text'The text has changed.'
is the string (the text) that we will write in the tagsFinally, we replace the text contained between the tags..
The table below lists some of the most common events:
Event | Description |
---|---|
onchange |
An HTML element has been modified |
onclick |
The user clicked on an HTML element |
oncontextmenu |
User opens a context menu (right click) |
onmouseover |
Mouse enters the element |
onmouseout |
Mouse leaves the element |
onkeydown |
The user presses a key on the keyboard |
onload |
The browser has finished loading the page |
In the HTML code below, add an event that opens an alert when the user click
on the image. The alert should display the text: Do not touch Chuck Norris
.
<img src="https://lucidar.me/en/web-dev-class/files/chuck-norris.jpg" height="300">
Here is the expected result:
Complete the code from exercise 1 so that an alert displays Image protected by Chuck Norris.
if the user
tries to open the context menu of the image (right click). Here is the expected result:
Pour éviter que le menu ne s'ouvre en plus de l'alerte, il faut ajouter une deuxième instruction : return false;
.
N'oubliez pas de séparer les instructions par des points-virgules.
Write an HTML page according to the template below. When the user clicks to see the answer, the text is replaced by the answer. Here is the expected result:
Modify the code of the previous exercise so that:
What language is used to describe the content of web pages?
What language is used to describe the formatting of web pages?
What language is used to manage the interaction on the web page?
What language is used to program a web server?
Which attribute triggers an event when a key is pressed on the keyboard?
onkeydown
triggers the event when the key is pushed.
Try again...
In the code below, what does this
refer to?
<p onclick="this.innerText='Text has changed.' ">
Click here to change the text.
</p>
onclick
.<p></p>
.<p></p>
.this
is the current element. In our case, it's the entire tag.
Try again...