function updateElementContent(elementId, content, type = 'text') { const element = document.getElementById(elementId); if (!element) { console.error(`Element with ID '${elementId}' not found.`); return; } if (type === 'html') { element.innerHTML = content; } else { // Default to 'text' for safety against XSS element.textContent = content; } } // Example usage: //
Initial text
// updateElementContent('myParagraph', 'New text content updated.'); updateElementContent('myDiv', 'This is bold HTML! And a red span.', 'html'); // Be cautious with innerHTML to prevent XSS vulnerabilities if content comes from user input: // const userInput = "