LearnFrenzy
Sign in Take a free test

DOM Manipulation · Exercise 2 of 4

DOM Manipulation Predict the Output

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

Each question shows a small HTML page and a script that runs on it. Choose what the script prints. Every script was run inside its page (jsdom on Node.js, as a browser script), so the answers are the real output.

Question 1 Medium

The page contains this HTML:

<ul id="menu">
  <li>Home</li>
  <li>Courses</li>
</ul>

What does the following JavaScript code print when it runs on this page?

main.js
const menu = document.getElementById("menu");
console.log(menu.childNodes.length, menu.children.length);
A 2 2
B 3 2
C 2 5
D 5 2
Question page & discussion
Question 2 Easy

The page contains this HTML:

<p id="msg">Hello <b>World</b>!</p>

What does the following JavaScript code print when it runs on this page?

main.js
const msg = document.getElementById("msg");
console.log(msg.textContent);
console.log(msg.innerHTML);
A Hello <b>World</b>! Hello World!
B Hello World! Hello World!
C Hello World! Hello <b>World</b>!
D Hello ! Hello <b>World</b>!
Question page & discussion
Question 3 Medium

The page contains this HTML:

<ul id="tasks">
  <li class="task done">Read</li>
  <li class="task">Code</li>
  <li class="task done">Test</li>
</ul>

What does the following JavaScript code print when it runs on this page?

main.js
const staticList = document.querySelectorAll(".task");
const liveList = document.getElementsByClassName("task");
const li = document.createElement("li");
li.className = "task";
document.getElementById("tasks").append(li);
console.log(staticList.length, liveList.length);
A 4 4
B 3 3
C 3 4
D 4 3
Question page & discussion
Question 4 Medium

The page contains this HTML:

<button id="b" class="btn">Menu</button>

What does the following JavaScript code print when it runs on this page?

main.js
const b = document.getElementById("b");
const first = b.classList.toggle("open");
const second = b.classList.toggle("open");
const forced = b.classList.toggle("open", true);
console.log(first, second, forced, b.className);
A false true true btn open
B true false false btn
C true true true btn open open
D true false true btn open
Question page & discussion
Question 5 Medium

The page contains this HTML:

<div id="u" data-user-id="42" data-is-admin="false"></div>

What does the following JavaScript code print when it runs on this page?

main.js
const { userId, isAdmin } = document.getElementById("u").dataset;
console.log(userId + 1, typeof userId, isAdmin ? "admin" : "user");
A 43 number user
B 421 string admin
C 421 string user
D 43 string admin
Question page & discussion
Question 6 Medium

The page contains this HTML:

<input id="q" value="old">

What does the following JavaScript code print when it runs on this page?

main.js
const q = document.getElementById("q");
q.value = "new";
console.log(q.value, q.getAttribute("value"), q.defaultValue);
A new new new
B old old old
C new new old
D new old old
Question page & discussion
Question 7 Easy

The page contains this HTML:

<section id="outer">
  <div id="inner"><button id="btn">Click</button></div>
</section>

What does the following JavaScript code print when it runs on this page?

main.js
const order = [];
for (const id of ["outer", "inner", "btn"]) {
  document.getElementById(id).addEventListener("click", () => order.push(id));
}
document.getElementById("btn").click();
console.log(order.join(" > "));
A outer > inner > btn
B btn > inner > outer
C btn
D inner > btn > outer
Question page & discussion
Question 8 Medium

The page contains this HTML:

<section id="outer">
  <div id="inner"><button id="btn">Click</button></div>
</section>

What does the following JavaScript code print when it runs on this page?

main.js
const order = [];
const on = (id, opts) => document.getElementById(id).addEventListener("click", () => order.push(id), opts);
on("btn");
on("inner");
on("outer", { capture: true });
document.getElementById("btn").click();
console.log(order.join(" > "));
A outer > btn > inner
B btn > inner > outer
C outer > inner > btn
D btn > outer > inner
Question page & discussion
Question 9 Medium

The page contains this HTML:

<section id="outer">
  <div id="inner"><button id="btn">Click</button></div>
</section>

What does the following JavaScript code print when it runs on this page?

main.js
const order = [];
document.getElementById("outer").addEventListener("click", () => order.push("outer"));
document.getElementById("inner").addEventListener("click", (e) => {
  order.push("inner");
  e.stopPropagation();
});
document.getElementById("inner").addEventListener("click", () => order.push("inner 2"));
document.getElementById("btn").click();
console.log(order.join(", "));
A inner
B inner, inner 2
C inner, inner 2, outer
D inner, outer
Question page & discussion
Question 10 Medium

The page contains this HTML:

<ul id="menu">
  <li>Home</li>
  <li>Courses</li>
</ul>

What does the following JavaScript code print when it runs on this page?

main.js
const menu = document.getElementById("menu");
menu.addEventListener("click", (e) => {
  console.log(e.target.tagName, e.currentTarget.tagName, e.target.textContent);
});
menu.children[1].click();
A UL UL Courses
B LI LI Courses
C UL LI Home
D LI UL Courses
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links