How TO - Toggle/Swap Text
Learn how to toggle text with JavaScript.
Hello
Toggle Text of an Element
Step 1) Add HTML:
Example
<button onclick="myFunction()">Click Me</button>
<div id="myDIV">Hello</div>
Step 2) Add JavaScript:
Example
function myFunction() {
var x = document.getElementById("myDIV");
if (x.innerHTML === "Hello") {
x.innerHTML = "Swapped
text!";
} else {
x.innerHTML = "Hello";
}
}
Try it Yourself »
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.