JavaScript – Change Text in Paragraph
To change the text in a paragraph using JavaScript, get reference to the paragraph element, and assign new text as string value to the innerHTML
property of the paragraph element.
Example
</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<h2>Change text in Paragraph using JavaScript</h2>
<p id="myPara">This is a paragraph.</p>
<button type="button" onclick="changeText()">Click Me</button>
<script>
function changeText(){
var element = document.getElementById("myPara");
element.innerHTML = "Hello World!";
}
</script>
</body>
</html>
Conclusion
In this JavaScript Tutorial, we learned how to change the text in a paragraph using JavaScript.