changing the text of p tag in HTML ...
changing the text of p tag in HTML ...
DOCTYPE html>
<html>
<head>
<title>Toggle P Tag Text</title>
<style>
/* Optional styling for visual feedback */
.clicked {
font-weight: bold;
color: blue;
}
</style>
</head>
<body>
<div id="myContainer">
<p id="myParagraph">Initial Text</p>
</div>
<script>
const myParagraph = document.getElementById('myParagraph');
const initialText = myParagraph.textContent; // Store the initial text
const newText = "Text has been changed!"; // The text to switch to
myParagraph.addEventListener('click', function() {
if (myParagraph.textContent === initialText) {
myParagraph.textContent = newText;
myParagraph.classList.add("clicked"); //Optional class for visual
feedback
} else {
myParagraph.textContent = initialText;
myParagraph.classList.remove("clicked"); //Optional class for
visual feedback
}
});
</script>
</body>
</html>