How to change the HTML content of an element using JavaScript?

To change the HTML content of an element using JavaScript, you can use the innerHTML property. Here's an example:

HTML:

<div id="myElement">Initial text</div>

JavaScript:

// Get the element using its ID var element = document.getElementById("myElement"); // Change the HTML content of the element element.innerHTML = "New text";

After running this script, the HTML content of the <div> with the ID "myElement" will be changed to "New text".