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:

  1. An element with the id `header` (use `getElementById`).
  2. All elements with the class `btn` (use `querySelectorAll`).
  3. 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`:

  1. Change its inner text to "Hello, World" (innerText).
  2. 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:

  1. Set the background color to red (backgroundColor).
  2. Set the font size to 20px (fontSize).
  3. 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:

  1. Add the `active` class.
  2. Remove the `hidden` class.
  3. 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:

  1. Create a new `li` element.
  2. Set its text to "New item".
  3. 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:

  1. Add a click event listener.
  2. 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:

  1. You have an `input` field and a button.
  2. On button click, read the field value (`value`).
  3. 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:

  1. Add a `keydown` listener on the document.
  2. 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:

  1. You have a `form` element.
  2. On submit, prevent page reload using `event.preventDefault()`.
  3. 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:

  1. When the `item` element is clicked, remove it from the DOM.
  2. Use `item.remove()` or `item.parentElement.removeChild(item)`.
Solution
const item = document.getElementById("item");

item.addEventListener("click", function() {
    item.remove();
});
Smart Editor

Write code and see the result instantly

Try it free