codejump

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.getElementById("id")
Bdocument.querySelector("#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)
AAll of the above
Belement.textContent = "New text";
Celement.innerText = "New text";
Delement.innerHTML = "New text";

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.class = "new-class";
Delement.classList.add("new-class");

Sign up free to play

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