LearnFrenzy
Sign in Take a free test

DOM Manipulation · Exercise 3 of 4

DOM Manipulation Find the Error

  • 15 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen DOM scripts, each shown with its page: SyntaxErrors from optional-chaining assignment, dashed dataset names and unbalanced listeners; runtime errors from null selections, wrong getElementById arguments, collection methods, non-Node arguments and string data values; and silent bugs from innerHTML XSS, lost listeners, e.target in delegation, live collections, className checks and missing units.

Question 11 Medium

The page contains this HTML:

<div id="post"><button id="like">Like</button></div>

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let likes = 0;
document.getElementById("like").addEventListener("click", () => likes++);
document.getElementById("post").innerHTML += "<p>Posted 2 min ago</p>";
document.getElementById("like").click();
document.getElementById("like").click();
console.log("likes:", likes);
A click() does not trigger listeners
B likes must be declared with var
C innerHTML += rebuilds the post, so the button is a new element without the listener – use insertAdjacentHTML("beforeend", …) or append
D The listener must be added after the innerHTML change only
Question page & discussion
Question 12 Hard

The page contains this HTML:

<ul id="list"><li data-id="7"><span>Revise arrays</span></li></ul>

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
document.getElementById("list").addEventListener("click", (e) => {
  console.log("delete item", e.target.dataset.id);
});
document.querySelector("#list span").click();     // the user clicks the text
A dataset.id must be dataset.ID
B e.target is the inner <span>, which has no data-id – use e.target.closest("li").dataset.id
C The listener should be on the span
D click() does not bubble
Question page & discussion
Question 13 Hard

The page contains this HTML:

<ul id="t"><li class="done">A</li><li class="done">B</li><li class="done">C</li><li class="done">D</li><li>E</li></ul>

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const done = document.getElementsByClassName("done");
for (let i = 0; i < done.length; i++) {
  done[i].remove();
}
console.log("left:", document.getElementById("t").textContent);
A The live collection shrinks as items are removed, so the loop skips every other item – loop over a static copy ([...done] or querySelectorAll)
B remove() only works on the last child
C i should start at 1
D getElementsByClassName needs a dot: ".done"
Question page & discussion
Question 14 Easy

The page contains this HTML:

<button id="tab" class="tab active">Profile</button>

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const tab = document.getElementById("tab");
const isActive = tab.className === "active";
console.log("is active?", isActive);
A className is the whole string "tab active" – use tab.classList.contains("active")
B className must be class
C The button needs an id of active
D === should be =
Question page & discussion
Question 15 Medium

The page contains this HTML:

<div id="bar" style="width: 10%;"></div>

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const bar = document.getElementById("bar");
const percent = 75;
bar.style.width = percent;               // update the progress
console.log("width is now:", bar.style.width);
A A number without a unit is invalid CSS, so the assignment is ignored – write bar.style.width = percent + "%"
B style.width is read-only
C percent must be a string "75"
D The inline style attribute blocks JavaScript changes
Question page & discussion
Up next · Exercise 4DOM Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links