The Document Object Model (DOM) is a cross-platform programming interface that treats HTML and XML documents as a hierarchical tree structure of nodes and objects. JavaScript uses DOM APIs to interact with web pages dynamically allowing scripts to select elements, update text contents, modify inline styles, manage CSS classes, create or remove node elements, and handle user interactions like clicks and keystrokes. Mastering selection methods (querySelector, getElementById), content properties (textContent, innerHTML), event handling (addEventListener), and element insertion is crucial for modern front-end development and technical software engineering interviews.
JavaScript DOM Manipulation MCQs
1 min read
Correct Answer: a) Document Object Model
Explanation:
DOM stands for Document Object Model, which represents the page so that programs can change the document structure, style, and content.
Correct Answer: b) querySelector()
Explanation:
querySelector() returns the first Element within the document that matches the specified CSS selector, or null if no matches are found.
Correct Answer: b) A static NodeList
Explanation:
querySelectorAll() returns a static NodeList, meaning subsequent changes to the DOM do not affect the contents of the collection.
Correct Answer: b) A live HTMLCollection
Explanation:
HTMLCollections returned by getElementsByClassName and getElementsByTagName are live, meaning they automatically update when elements are added or removed.
Correct Answer: c) innerHTML
Explanation:
innerHTML gets or sets the HTML markup contained within the element.
Correct Answer: a) innerText is aware of CSS styling and won't return hidden text, whereas textContent returns all text regardless of visibility.
Explanation:
innerText takes CSS styling into account and triggers a reflow, while textContent gets the raw text of all nodes without rendering checks.
Correct Answer: b) document.createElement()
Explanation:
document.createElement() creates the specified HTML element specified by tagName.
Correct Answer: a) parent.appendChild(child)
Explanation:
appendChild() adds a node to the end of the list of children of a specified parent node.
Correct Answer: c) Both parentElement and parentNode
Explanation:
Both properties return the parent of the specified node, with parentElement returning null if the parent is not an Element node.
Correct Answer: c) It adds the class if it is missing, or removes it if it is already present.
Explanation:
classList.toggle() behaves like a switch: adding the class if absent and removing it if present.
Correct Answer: a) element.remove()
Explanation:
The Element.remove() method removes the element from the DOM children list of its parent.
Correct Answer: b) element.addEventListener()
Explanation:
addEventListener() is the standard W3C DOM method for registering an event handler on an element.
Correct Answer: a) Events propagate from the innermost target element outward up to the root document.
Explanation:
Event bubbling is the phase where an event triggered on a child element bubbles up through its ancestors in the DOM tree.
Correct Answer: a) Events propagate from the root document down to the target element before bubbling.
Explanation:
During the capturing phase, the event starts from the window/document and travels down to the target element.
Correct Answer: b) event.stopPropagation()
Explanation:
stopPropagation() prevents further propagation of the current event in the bubbling or capturing phase.
Correct Answer: b) Cancels the event if it is cancelable, meaning the default action belonging to the event will not occur
Explanation:
preventDefault() stops browser default behaviors, such as following a link or submitting a form.
Correct Answer: a) Delegating event handling to a single parent element to manage events on multiple child elements efficiently
Explanation:
Event delegation leverages event bubbling to handle events at a higher level in the DOM, reducing memory overhead and supporting dynamic elements.
Correct Answer: b) event.target
Explanation:
event.target points to the DOM element that dispatched the event (the actual source where the user clicked/interacted).
Correct Answer: a) The element whose event listener is currently being traversed (the element attached to the listener)
Explanation:
event.currentTarget always refers to the element to which the event handler has been attached.
Correct Answer: b) To act as a lightweight, off-DOM container to batch multiple insertions and prevent frequent reflows/repaints
Explanation:
DocumentFragment is not part of the main DOM tree. Appending children to it does not trigger reflow, making batch insertions highly performant.
Correct Answer: b) getBoundingClientRect()
Explanation:
getBoundingClientRect() returns the bounding box properties (top, left, bottom, right, width, height) relative to the viewport.
Correct Answer: b) element.setAttribute('name', 'value')
Explanation:
setAttribute() sets the value of an attribute on the specified element.
Correct Answer: a) element.getAttribute('name')
Explanation:
getAttribute() returns the value of a specified attribute on the element.
Correct Answer: a) element.getAttribute('data-user-id') or element.dataset.userId
Explanation:
Custom data attributes are accessible via getAttribute or camelCase properties on the element.dataset object.
Correct Answer: a) The process of recalculating the positions and geometries of elements in the document for rendering
Explanation:
Reflow is the heavy computational process where the browser calculates layout geometries for element rendering.
Correct Answer: a) Redrawing elements when their visual styling changes without affecting layout geometry
Explanation:
Repaint occurs when changes are made to element styling (like background color or visibility) that don't alter layout dimensions.
Correct Answer: b) children
Explanation:
The children property returns only element nodes, whereas childNodes returns all child nodes including text, whitespace, and comments.
Correct Answer: a) element.hasChildNodes()
Explanation:
hasChildNodes() returns a boolean value indicating whether the given node has child nodes.
Correct Answer: a) element.append()
Explanation:
Element.append() allows appending multiple nodes and strings (unlike appendChild which only accepts a single Node object and returns it).
Correct Answer: a) Inserts nodes or strings before the first child of the element
Explanation:
prepend() inserts a set of Node or string objects before the first child of the parent element.
Correct Answer: d) Both a and c
Explanation:
Both parent.replaceChild() and modern element.replaceWith() can be used to replace DOM nodes.
Correct Answer: a) A boolean (true if the element would be selected by the specified CSS selector string, otherwise false)
Explanation:
element.matches() checks if the element is selectable by the specified CSS selector query.
Correct Answer: a) element.closest()
Explanation:
element.closest() traverses the element and its parents (heading toward the document root) until it finds a node matching the provided selector.
Correct Answer: a) window.getComputedStyle(element)
Explanation:
window.getComputedStyle() returns an object containing the computed values of all CSS properties of an element.
Correct Answer: a) It only retrieves inline styles set directly on the element's style attribute, not styles from external or internal CSS stylesheets.
Explanation:
element.style only reflects inline styles, which is why getComputedStyle is required for stylesheets.
Correct Answer: a) To set keyboard focus on the specified element
Explanation:
element.focus() gives focus to the element if it can be focused (such as input fields, buttons, or elements with tabindex).
Correct Answer: a) button.click()
Explanation:
The HTMLElement.click() method simulates a mouse click on an element.
Correct Answer: a) const event = new CustomEvent('myevent', { detail: { id: 1 } }); element.dispatchEvent(event);
Explanation:
CustomEvent constructor and dispatchEvent() are the standard APIs for creating and emitting custom application events.
Correct Answer: a) It signals to the browser that the listener will never call preventDefault(), improving scroll performance.
Explanation:
Passive event listeners allow browsers to immediately scroll without waiting for touch/wheel event handlers to finish executing.
Correct Answer: a) It automatically removes the event listener after it is triggered for the first time.
Explanation:
Setting once: true ensures the listener is unregistered automatically right after its first invocation.
Correct Answer: a) offsetHeight
Explanation:
offsetHeight returns the outer height of an element, including CSS height, padding, and borders, plus horizontal scrollbar if rendered.
Correct Answer: a) Inner height of an element including padding, but excluding borders, margins, and horizontal scrollbars
Explanation:
clientHeight represents the inner height of an element in pixels, including padding but excluding borders and margins.
Correct Answer: a) The entire content height of an element, including content not visible on the screen due to overflow
Explanation:
scrollHeight measures the total height of element content including overflow content that requires scrolling.
Correct Answer: a) scrollTop
Explanation:
scrollTop gets or sets the number of pixels that an element's content is scrolled vertically.
Correct Answer: a) checkbox.checked
Explanation:
The checked boolean property accurately reflects whether a checkbox or radio button is currently checked.
Correct Answer: b) input
Explanation:
The input event fires synchronously whenever the value of an input, select, or textarea element is changed by user interaction.
Correct Answer: b) When the element loses focus after its value has been changed (or when a selection is committed)
Explanation:
Unlike the input event, the change event typically fires when the control loses focus or when a commit action occurs (e.g., selecting a dropdown option).
Correct Answer: b) event.preventDefault() inside the form's 'submit' event listener
Explanation:
Calling event.preventDefault() inside a submit event listener prevents the browser from performing its default form submission request.
Correct Answer: a) It creates a deep clone of the element, including all of its descendants and child nodes.
Explanation:
Passing true to cloneNode() performs a deep clone; passing false (or omitting it) performs a shallow clone of only the node itself.
Correct Answer: a) nodeType
Explanation:
nodeType returns an integer representing the type of the node (Node.ELEMENT_NODE is 1, Node.TEXT_NODE is 3, etc.).
Correct Answer: a) P
Explanation:
nodeName for HTML elements returns their tag name in uppercase (e.g., 'DIV', 'P', 'SPAN').
Correct Answer: a) element.insertAdjacentHTML(position, text)
Explanation:
insertAdjacentHTML parses the specified text as HTML and inserts the resulting nodes into the DOM tree at a specified position.
Correct Answer: a) A boolean indicating whether the element has the specified attribute
Explanation:
hasAttribute() returns true if the attribute is present on the element, and false otherwise.
Correct Answer: a) element.removeAttribute('name')
Explanation:
removeAttribute() removes the attribute with the specified name from the element.
Correct Answer: a) It fires when the whole page has loaded, including all dependent resources such as stylesheets and images.
Explanation:
window 'load' waits for complete asset loading, unlike DOMContentLoaded which fires earlier.
Correct Answer: a) When the initial HTML document has been completely parsed and the DOM tree is built, without waiting for stylesheets, images, and subframes to finish loading.
Explanation:
DOMContentLoaded is ideal for running scripts that interact with DOM elements as soon as the markup is parsed.
Correct Answer: a) parent.contains(child)
Explanation:
Node.contains() returns a boolean value indicating whether a node is a descendant of a specified node.
Correct Answer: a) previousSibling
Explanation:
previousSibling returns the previous node in the tree, which may be a text node or whitespace.
Correct Answer: a) nextSibling returns any node (including whitespace/text), while nextElementSibling skips non-element nodes to return the next sibling Element.
Explanation:
Element-specific traversal properties ignore text and comment nodes.
Correct Answer: a) window
Explanation:
The window object represents the browser window containing the DOM document.
Correct Answer: a) document.documentElement
Explanation:
document.documentElement is a read-only property that returns the root element of the document (such as for HTML documents).
Correct Answer: c) Both a and b
Explanation:
document.body is a direct shortcut property to access the element.
Correct Answer: a) The element of the current document
Explanation:
document.head returns the element of the document.
Correct Answer: a) element.classList.length > 0
Explanation:
classList implements a DOMTokenList, which has a length property representing the number of classes applied.
Correct Answer: a) classList.contains('className')
Explanation:
classList.contains() returns true if the element's class list contains the specified class name.
Correct Answer: a) To remove keyboard focus from the specified element
Explanation:
element.blur() removes focus from the currently focused element.
Correct Answer: a) select
Explanation:
The select event fires when text has been selected in a or element.
Correct Answer: a) It fires when a form is submitted (via button click or enter key press in input fields).
Explanation:
The submit event fires on a form when it is submitted.
Correct Answer: a) form.reset()
Explanation:
The HTMLFormElement.reset() method restores a form's default values.
Correct Answer: a) Elements or windows when their viewable content is scrolled
Explanation:
The scroll event fires when the document view or an element has been scrolled.
Correct Answer: a) To scroll the window to a particular set of coordinates in the document
Explanation:
window.scrollTo scrolls the window to absolute coordinates (x, y).
Correct Answer: a) window.scrollBy(x, y)
Explanation:
window.scrollBy scrolls the document by the specified number of pixels relative to its current scroll position.
Correct Answer: a) Scrolls the parent container of the element so that the element is visible in the browser viewport
Explanation:
scrollIntoView scrolls the element's parent container until the element is brought into the visible area of the browser.
Correct Answer: a) It fires when the browser window has been resized.
Explanation:
The resize event is sent to the window when the document view (window) is resized.
Correct Answer: a) window.getComputedStyle(element).display === 'none' or element.hidden
Explanation:
Checking computed display property or the hidden boolean property determines visibility.
Correct Answer: a) display: none removes the element from layout calculation entirely, whereas visibility: hidden hides the element but preserves its layout space.
Explanation:
display: none causes reflow by removing the element from the document layout, while visibility: hidden leaves an empty structural gap.
Correct Answer: c) Both a and b
Explanation:
querySelectorAll returns a static NodeList, while getElementsByClassName returns a live HTMLCollection.
Correct Answer: a) The string ID of the element without the '#' symbol
Explanation:
getElementById takes the ID string directly without any leading hash '#' symbol.
Correct Answer: a) removeEventListener()
Explanation:
removeEventListener removes an event listener previously registered with addEventListener.
Correct Answer: a) Because JavaScript identifies listeners by reference; anonymous functions cannot be removed because their references cannot be addressed.
Explanation:
To remove a listener, the exact function reference used during addition must be passed to removeEventListener.
Correct Answer: a) Returns the time (in milliseconds) at which the event was created
Explanation:
timeStamp represents the epoch time when the event was generated.
Correct Answer: a) Clears all child elements and text content inside the element efficiently
Explanation:
Setting innerHTML to an empty string wipes out all internal descendants of the element.
Correct Answer: a) form.elements
Explanation:
form.elements returns an HTMLFormControlsCollection of all form controls contained in the element.
Correct Answer: a) By querying checked inputs, e.g., document.querySelector('input[name="gender"]:checked').value
Explanation:
Using a CSS pseudo-class :checked selector is the standard way to retrieve the active radio button's value.
Correct Answer: a) false
Explanation:
element.matches() returns true or false based on whether the selector matches the element.
Correct Answer: a) The input field has lost focus.
Explanation:
The blur event fires when an element has lost focus.
Correct Answer: a) The input field has received focus.
Explanation:
The focus event fires when an element has received focus.
Correct Answer: a) It avoids attaching individual event listeners to every single item, saving memory and supporting new items automatically.
Explanation:
Event delegation reduces listener overhead and automatically handles dynamically injected elements.
Correct Answer: a) element.disabled (returns true if disabled)
Explanation:
The disabled property reflects whether form control elements are disabled.
Correct Answer: a) -1 or 0 depending on whether the element is naturally focusable
Explanation:
tabIndex returns the tab order of the element.
Correct Answer: a) input.value
Explanation:
Form input controls store user entries in their .value property, not textContent or innerHTML.
Correct Answer: a) parent.insertBefore(newNode, referenceNode)
Explanation:
insertBefore() inserts a node before the reference node as a child of a specified parent.
Correct Answer: a) The newNode is inserted at the end of the child list (acting like appendChild).
Explanation:
According to the DOM specification, if referenceNode is null, newNode is inserted at the end of the child nodes.
Correct Answer: a) Opens a document stream for writing (document.write)
Explanation:
document.open() opens a document stream for writing via document.write().
Correct Answer: a) It can wipe out the entire document if called after the page has finished loading, and harms performance.
Explanation:
document.write blocks parsing and can overwrite loaded DOM contents if invoked post-load.
Correct Answer: a) document.activeElement === element
Explanation:
document.activeElement returns the DOM element that currently has focus.
Correct Answer: a) Returns a Selection object representing the range of text selected by the user
Explanation:
window.getSelection() represents the text selection highlight made by the user in the document.
Correct Answer: a) window.getComputedStyle(element).fontSize
Explanation:
Computed font size is retrieved via window.getComputedStyle.
Correct Answer: a) classList provides convenient helper methods like add, remove, toggle, and contains without needing manual string parsing.
Explanation:
classList avoids string manipulation errors when managing multiple CSS classes on an element.
Correct Answer: a) The string value of the element's id attribute
Explanation:
The id property gets or sets the element's identifier.
Correct Answer: a) Yes
Explanation:
Multiple classes can be assigned separated by whitespace in className.
Correct Answer: a) "A"
Explanation:
tagName returns the element's tag name in uppercase string format.
Correct Answer: c) Both a and b
Explanation:
Both childElementCount and children.length provide the count of child element nodes.
Correct Answer: a) The ability to dynamically build interactive user interfaces, handle user events, and optimize browser rendering performance
Explanation:
DOM mastery is essential for building dynamic web apps and front-end architectures.
Related Posts
New
New
New

Top Python Fundamentals MCQs & Answers for Beginners
Python is a dynamically typed, high-level programming language created by Guido van Rossum in 1991. Renowned for its clear syntax…
August 27, 2026By MCQs Generator

JavaScript Asynchronous Programming
Asynchronous programming in JavaScript enables non-blocking execution, allowing long-running operations such as network requests, file reading, or timers to run…
August 29, 2026By MCQs Generator

Python Functions MCQs
Functions in Python are reusable blocks of code designed to perform a specific task, promoting code modularity, readability, and DRY…
August 27, 2026By MCQs Generator
Related Categories
New












AI & Data Science MCQ
5 topics
By MCQs Generator
New
Arts & Humanities MCQ
4 topics
By MCQs Generator
New
Civil Engineering MCQ
4 topics
By MCQs Generator
New
Commerce & Business MCQ
4 topics
By MCQs Generator
New
Competitive Exams MCQ
5 topics
By MCQs Generator
New
Electrical & Electronics Engineering MCQ
3 topics
By MCQs Generator
New
General Knowledge MCQ
2 topics
By MCQs Generator
New
General Science MCQ
4 topics
By MCQs Generator
New
Law & Judiciary MCQ
3 topics
By MCQs Generator
New
Mechanical Engineering MCQ
4 topics
By MCQs Generator
New
Medical & Health Sciences MCQ
4 topics
By MCQs Generator
New
Modern Tech Fields MCQ
3 topics
By MCQs Generator