By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
DOM manipulation is the process of interacting with and modifying the Document Object Model (DOM) of a web page. This involves using JavaScript to dynamically change the structure, style, and content of HTML elements. getElementById, querySelector, innerHTML, and textContent are fundamental methods for this task. Mastering these techniques is crucial for creating interactive and responsive web applications. Incorrect usage can lead to broken functionality, poor user experience, and security vulnerabilities like cross-site scripting (XSS). For example, improperly setting innerHTML can expose your application to XSS attacks, compromising user data.
document.getElementById('myElement')
⚠️ Common Pitfall: Duplicate IDs can cause unpredictable behavior.
Accessing Elements with querySelector
document.querySelector('.myClass')
⚠️ Common Pitfall: Only the first matching element is returned.
Modifying Content with innerHTML
element.innerHTML = '<p>New Content</p>'
⚠️ Common Pitfall: Can introduce XSS vulnerabilities if user input is not sanitized.
Modifying Content with textContent
element.textContent = 'New Text'
Experts view DOM manipulation as a toolkit for creating dynamic and responsive user interfaces. They prioritize security and performance, using textContent over innerHTML when possible to avoid XSS risks. They also understand the trade-offs between different selection methods and choose the most efficient one for the task at hand.
Exam trap: Questions with duplicate IDs to test understanding.
The mistake: Assuming querySelector returns all matching elements.
Exam trap: Questions requiring selection of multiple elements.
The mistake: Setting innerHTML with unsanitized user input.
Exam trap: Scenarios involving user input and innerHTML.
The mistake: Expecting textContent to render HTML tags.
Scenario: You need to update a paragraph with the ID "description" to display user-generated content.Question: How do you safely update the paragraph? Solution: 1. Select the paragraph using getElementById.2. Use textContent to set the new content.Answer: document.getElementById('description').textContent = 'User-generated content'; Why it works: textContent avoids XSS risks by not interpreting HTML tags.
document.getElementById('description').textContent = 'User-generated content';
Scenario: You need to change the background color of all elements with the class "highlight".Question: How do you select and modify these elements? Solution: 1. Use querySelectorAll to select all elements with the class "highlight".2. Loop through the NodeList and change the background color.Answer:
document.querySelectorAll('.highlight').forEach(element => { element.style.backgroundColor = 'yellow'; });
Why it works: querySelectorAll returns all matching elements, allowing you to modify each one.
Scenario: You need to insert a new list item into an unordered list with the ID "myList".Question: How do you add the new list item? Solution: 1. Select the unordered list using getElementById.2. Create a new list item element.3. Set the textContent of the new list item.4. Append the new list item to the unordered list.Answer:
const list = document.getElementById('myList'); const newItem = document.createElement('li'); newItem.textContent = 'New List Item'; list.appendChild(newItem);
Why it works: createElement and appendChild allow you to dynamically add new elements to the DOM.
document.querySelectorAll('.class')
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.