// Get a reference to the list container const listContainer = document.getElementById('myList'); if (listContainer) { // Create a DocumentFragment const fragment = document.createDocumentFragment(); // Generate multiple list items for (let i = 1; i <= 5; i++) { const listItem = document.createElement('li'); listItem.textContent = `List Item ${i} (added via fragment)`; fragment.appendChild(listItem); // Append to fragment, not directly to DOM } // Append the entire fragment to the DOM in one go listContainer.appendChild(fragment); console.log('5 list items added efficiently using DocumentFragment.'); } else { console.error('List container with ID "myList" not found.'); } /* HTML structure for testing: