/** * Dynamically creates a new paragraph element, sets its text content and a class, * and then appends it to a specified parent element in the DOM. */ function addParagraphToContainer(containerId, textContent, className) { const container = document.getElementById(containerId); if (container) { const newParagraph = document.createElement('p'); newParagraph.textContent = textContent; newParagraph.classList.add(className); container.appendChild(newParagraph); console.log(`Added new paragraph: "${textContent}" to #${containerId}`); } else { console.error(`Container with ID "${containerId}" not found.`); } } // Example Usage (assuming an HTML element with id='myContainer' exists): //
// addParagraphToContainer('myContainer', 'This is a new paragraph!', 'dynamic-text');