Change Text Dynamically

 To change the text dynamically using JavaScript, you can use the innerHTML property of a DOM element. Here's an example:

HTML:

php
<p id="myText">This is some text.</p> <button onclick="changeText()">Click me</button>

JavaScript:

javascript
function changeText() { document.getElementById("myText").innerHTML = "This text has been changed."; }

In this example, we have a paragraph element with the ID myText and a button that calls the changeText function when clicked. The changeText function uses the document.getElementById method to get a reference to the myText element and then sets its innerHTML property to the new text we want to display.

Comments