Test your skills: Events
The aim of this skill test is to assess whether you've understood our Introduction to events article.
Note: You can try solutions by downloading the code and putting it in an online editor such as CodePen or JSFiddle. If there is an error, it will be logged in the results panel on the page or into the browser's JavaScript console to help you.
If you get stuck, you can reach out to us in one of our communication channels.
DOM manipulation: considered useful
Some of the questions below require you to write some DOM manipulation code to complete them — such as creating new HTML elements, setting their text contents to equal specific string values, and nesting them inside existing elements on the page — all via JavaScript.
We haven't explicitly taught this yet in the course, but you'll have seen some examples that make use of it, and we'd like you to do some research into what DOM APIs you need to successfully answer the questions. A good starting place is our DOM scripting introduction tutorial.
Events 1
Our first events-related task involves a <button>
that, when clicked, updates its text label. The HTML should not be changed; just the JavaScript.
To complete the task:
- Click "Play" in the code block below to edit the example in the MDN Playground.
- Create an event listener that causes the text inside the button (
btn
) to change when it is clicked on, and change back when it is clicked again.
You can also download the starting point for this task to work in your own editor or in an online editor.
If you make a mistake, you can clear your work using the Reset button in the MDN Playground. If you get really stuck, you can view the solution below the live output.
const btn = document.querySelector("button");
// Add your code here
Click here to show the solution
Your finished JavaScript should look something like this:
const btn = document.querySelector("button");
btn.addEventListener("click", () => {
if (btn.className === "on") {
btn.textContent = "Machine is off";
btn.className = "off";
} else {
btn.textContent = "Machine is on";
btn.className = "on";
}
});
Events 2
Now we'll look at keyboard events.
To complete this task:
- Click "Play" in the code block below to edit the example in the MDN Playground.
- Create an event listener that moves the circle around the provided canvas when the WASD keys are pressed on the keyboard. The circle is drawn with the function
drawCircle()
, which takes the following parameters as inputs:x
— the x coordinate of the circle.y
— the y coordinate of the circle.size
— the radius of the circle.
Warning: When testing your code, you will have to focus on the canvas before trying out your keyboard commands (for example, click on it, or tab to it with the keyboard). Otherwise they won't work.
You can also download the starting point for this task to work in your own editor or in an online editor.
If you make a mistake, you can clear your work using the Reset button in the MDN Playground. If you get really stuck, you can view the solution below the live output.
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
function drawCircle(x, y, size) {
ctx.fillStyle = "white";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.beginPath();
ctx.fillStyle = "black";
ctx.arc(x, y, size, 0, 2 * Math.PI);
ctx.fill();
}
let x = 50;
let y = 50;
const size = 30;
drawCircle(x, y, size);
// Don't edit the code above here!
// Add your code here
Click here to show the solution
Your finished JavaScript should look something like this:
// ...
// Don't edit the code above here!
window.addEventListener("keydown", (e) => {
switch (e.key) {
case "a":
x -= 5;
break;
case "d":
x += 5;
break;
case "w":
y -= 5;
break;
case "s":
y += 5;
break;
}
drawCircle(x, y, size);
});
Events 3
In the next events-related task tests your knowledge of event bubbling.
To complete this task:
- Click "Play" in the code block below to edit the example in the MDN Playground.
- Set an event listener on the
<button>
s' parent element (<div class="button-bar"> … </div>
) which, when invoked by clicking any of the buttons, will set the background of thebutton-bar
to the color contained in the button'sdata-color
attribute.
We want you to solve this without looping through all the buttons and giving each one their own event listener.
You can also download the starting point for this task to work in your own editor or in an online editor.
If you make a mistake, you can clear your work using the Reset button in the MDN Playground. If you get really stuck, you can view the solution below the live output.
const buttonBar = document.querySelector(".button-bar");
// Add your code here
Click here to show the solution
Your finished JavaScript should look something like this:
const buttonBar = document.querySelector(".button-bar");
function setColor(e) {
buttonBar.style.backgroundColor = e.target.getAttribute("data-color");
}
buttonBar.addEventListener("click", setColor);