DOM & Events

Preview — 3 of 10 questions

Which method is used to select an element by its ID in JavaScript?

javascript
const heading = document.getElementById("main-heading");
console.log(heading.textContent); // Access the element
Adocument.querySelector("#id")
Bdocument.getElementById("id")
Cdocument.querySelector(".id")
Ddocument.getElementByClass("id")

How do you change the text content of an HTML element with JavaScript?

javascript
element.textContent = "Hello <b>World</b>";
// Renders as: "Hello <b>World</b>" (literal text)
Aelement.innerHTML = "New text";
Belement.textContent = "New text";
Celement.innerText = "New text";
DAll of the above

What is the correct way to add a new class to an element?

javascript
const button = document.querySelector("button");
button.classList.add("active");
// If button already had classes, they're preserved
Aelement.className = "new-class";
Belement.addClass("new-class");
Celement.classList.add("new-class");
Delement.class = "new-class";

Sign up free to play

Answer all 10 questions (7 more), see explanations for every answer, and track your score.