JS Exam 3: DOM and Events (10 mixed exercises)
JS Exam 3: DOM and Events
Test your skills in interacting with the page, updating its content, and responding to user actions.
Exercise 1
Selecting Elements
Write JS code to select the following elements:
- An element with the id `header` (use `getElementById`).
- All elements with the class `btn` (use `querySelectorAll`).
- The first `p` element on the page (use `querySelector`).
Solution
const header = document.getElementById("header");
const buttons = document.querySelectorAll(".btn");
const firstPara = document.querySelector("p");
Exercise 2
Changing Content
You have an `h1` element with id `title`:
- Change its inner text to "Hello, World" (innerText).
- Change its HTML to `<span>Hello</span>` (innerHTML).
Solution
const title = document.getElementById("title");
title.innerText = "Hello, World";
title.innerHTML = "<span>Hello</span>";
Exercise 3
Changing Styles
Update the styles of the `box` element:
- Set the background color to red (backgroundColor).
- Set the font size to 20px (fontSize).
- Hide the element entirely (display = 'none').
Solution
const box = document.getElementById("box");
box.style.backgroundColor = "red";
box.style.fontSize = "20px";
box.style.display = "none";
Exercise 4
Class Manipulation
Use `classList` to modify the `item` classes:
- Add the `active` class.
- Remove the `hidden` class.
- Toggle the `selected` class.
Solution
const item = document.getElementById("item");
item.classList.add("active");
item.classList.remove("hidden");
item.classList.toggle("selected");
Exercise 5
Creating Elements
Add a new element to the page:
- Create a new `li` element.
- Set its text to "New item".
- Append it to an existing list with id `list` (appendChild).
Solution
const newItem = document.createElement("li");
newItem.innerText = "New item";
const list = document.getElementById("list");
list.appendChild(newItem);
Exercise 6
Event Listeners
Make the `btn` button interactive:
- Add a click event listener.
- On click, log "Clicked!" to the console.
Solution
const btn = document.getElementById("btn");
btn.addEventListener("click", function() {
console.log("Clicked!");
});
Exercise 7
Input Values
Read a value from an input field:
- You have an `input` field and a button.
- On button click, read the field value (`value`).
- Display it in an alert.
Solution
const input = document.getElementById("myInput");
const btn = document.getElementById("myBtn");
btn.addEventListener("click", function() {
alert(input.value);
});
Exercise 8
Keyboard Events
Listen for key presses:
- Add a `keydown` listener on the document.
- Log the pressed key name (`event.key`) to the console.
Solution
document.addEventListener("keydown", function(event) {
console.log(event.key);
});
Exercise 9
Prevent Default
Prevent a form from submitting:
- You have a `form` element.
- On submit, prevent page reload using `event.preventDefault()`.
- Log "Submission blocked".
Solution
const form = document.getElementById("myForm");
form.addEventListener("submit", function(event) {
event.preventDefault();
console.log("Submission blocked");
});
Exercise 10
Removing Elements
Remove an element from the page:
- When the `item` element is clicked, remove it from the DOM.
- Use `item.remove()` or `item.parentElement.removeChild(item)`.
Solution
const item = document.getElementById("item");
item.addEventListener("click", function() {
item.remove();
});