To replace HTML content when a button is clicked in JavaScript is a straightforward process. You can follow these simple steps:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Replace Content Example</title>
</head>
<body>
<div id="content">This is the original content.</div>
<button id="replaceButton">Click me</button>
<script>
// Select the button and content element
const button = document.getElementById('replaceButton');
const content = document.getElementById('content');
// Add event listener to the button
button.addEventListener('click', function() {
// Replace the HTML content inside the div
content.innerHTML = 'The content has been replaced!';
});
</script>
</body>
</html>