Question 1
JavaScriptconst element = document.getElementById("title"); console.log(element.textContent);
getElementById() returns the element with the matching id.
Question 2
Aconst element = document.querySelector(".box"); console.log(element.textContent);
querySelector() returns the first matching element.
Question 3
const elements =
document.querySelectorAll(".item");
console.log(elements.length);
querySelectorAll() returns all matching elements.
Question 4
const cards =
document.getElementsByClassName("card");
console.log(cards.length);
getElementsByClassName() returns all matching elements.
Question 5
A
B
const elements = document.getElementsByTagName("p"); console.log(elements.length);
Returns all matching tag elements.
Question 6
const box =
document.getElementById("box");
box.innerHTML = "<span>Hello</span>";
console.log(box.innerHTML);
innerHTML returns HTML markup.
Question 7
Helloconst box = document.getElementById("box"); console.log(box.innerText);
innerText returns visible text.
Question 8
JavaScriptconst box = document.getElementById("box"); console.log(box.textContent);
textContent returns all text content.
Question 9
const box =
document.getElementById("box");
box.textContent =
"Hello";
console.log(box.textContent);
textContent treats everything as plain text.
Question 10
Oldconst title = document.getElementById("title"); title.textContent = "New"; console.log(title.textContent);
textContent updates the text inside the element.
Question 11
const box = document.getElementById("box"); console.log( box.getAttribute("data-role") );
getAttribute() returns the attribute value.
Question 12
const box =
document.getElementById("box");
box.setAttribute(
"title",
"Frontend"
);
console.log(
box.getAttribute("title")
);
setAttribute() adds or updates an attribute.
Question 13
const box = document.getElementById("box"); box.removeAttribute("title"); console.log( box.getAttribute("title") );
The attribute no longer exists.
Question 14
const box = document.getElementById("box"); console.log( box.hasAttribute("title") );
hasAttribute() checks whether an attribute exists.
Question 15
const box = document.getElementById("box"); console.log( box.dataset.user );
data-user becomes dataset.user.
Question 16
const element =
document.createElement("div");
console.log(
element.tagName
);
tagName returns the uppercase tag name in HTML documents.
Question 17
const parent =
document.getElementById("parent");
const child =
document.createElement("span");
parent.appendChild(child);
console.log(
parent.children.length
);
appendChild() adds a child node at the end.
Question 18
const parent =
document.getElementById("parent");
parent.append("JavaScript");
console.log(
parent.textContent
);
append() can append text directly.
Question 19
Bconst parent = document.getElementById("parent"); const child = document.createElement("span"); child.textContent = "A"; parent.prepend(child); console.log( parent.firstElementChild.textContent );
prepend() inserts at the beginning.
Question 20
- B
insertBefore() inserts before the specified node.
Question 21
const box =
document.getElementById("box");
box.remove();
console.log(
document.getElementById("box")
);
remove() deletes the element from the DOM.
Question 22
- A
removeChild() removes the specified child node.
Question 23
const box =
document.getElementById("box");
box.classList.add("active");
console.log(
box.classList.contains("active")
);
add() inserts the class into classList.
Question 24
const box = document.getElementById("box"); box.classList.remove("active"); console.log( box.classList.contains("active") );
remove() deletes the specified class.
Question 25
const box =
document.getElementById("box");
box.classList.toggle("active");
console.log(
box.classList.contains("active")
);
toggle() adds the class if it does not exist.
Question 26
const box = document.getElementById("box"); console.log( box.classList.contains("active") );
contains() checks class existence.
Question 27
const box =
document.getElementById("box");
box.style.color = "red";
console.log(
box.style.color
);
style.color returns the inline style value.
Question 28
const box =
document.getElementById("box");
box.style.display = "none";
console.log(
box.style.display
);
display stores the assigned CSS value.
Question 29
const box =
document.getElementById("box");
box.style.visibility = "hidden";
console.log(
box.style.visibility
);
visibility controls whether the element is visible.
Question 30
const box = document.getElementById("box"); console.log( getComputedStyle(box).color );
getComputedStyle() returns computed CSS values. Browsers typically return rgb(255, 0, 0), so avoid assuming exact formatting.
Question 31
const child = document.getElementById("child"); console.log( child.parentElement.id );
parentElement returns the parent DOM element.
Question 32
const box = document.getElementById("box"); console.log( box.children.length );
children includes only element nodes.
Question 33
Aconst box = document.getElementById("box"); console.log( box.firstElementChild.tagName );B
firstElementChild returns the first child element.
Question 34
const box = document.getElementById("box"); console.log( box.lastElementChild.tagName );
lastElementChild returns the last child element.
Question 35
const a = document.getElementById("a"); console.log( a.nextElementSibling.id );
nextElementSibling returns the next sibling element.
Question 36
const b = document.getElementById("b"); console.log( b.previousElementSibling.id );
previousElementSibling returns the previous sibling element.
Question 37
const div =
document.createElement("div");
div.textContent = "JS";
const clone =
div.cloneNode(true);
console.log(
clone.textContent
);
cloneNode(true) creates a deep clone.
Question 38
const parent = document.getElementById("parent"); const newNode = document.createElement("p"); parent.replaceChild( newNode, document.getElementById("old") ); console.log( parent.firstElementChild.tagName );
replaceChild replaces an existing node.
Question 39
const box =
document.getElementById("box");
box.insertAdjacentHTML(
"beforeend",
"<span>JS</span>"
);
console.log(
box.children.length
);
insertAdjacentHTML inserts parsed HTML.
Question 40
const fragment = document.createDocumentFragment(); console.log( fragment.nodeType );
DocumentFragment nodeType is 11.
Question 41
const items =
document.getElementsByClassName("item");
console.log(
items.constructor.name
);
getElementsByClassName returns an HTMLCollection.
Question 42
const items =
document.querySelectorAll(".item");
console.log(
items.constructor.name
);
querySelectorAll returns a NodeList.
Question 43
const items = document.getElementsByClassName("item"); const newDiv = document.createElement("div"); newDiv.className = "item"; document .getElementById("parent") .appendChild(newDiv); console.log(items.length);
HTMLCollection is live and updates automatically.
Question 44
Textconst box = document.getElementById("box"); console.log( box.childNodes.length > box.children.length );
childNodes includes text nodes. children includes only element nodes.
Question 45
const box = document.getElementById("box"); const children = box.children; box.appendChild( document.createElement("p") ); console.log( children.length );
children returns a live HTMLCollection. The length updates automatically.