How to make text disappear in JavaScript

To make text disappear, simply set its style.display to none.

text.style.display = "none";

How to make text disappear on button click?

Example:

<div id="myText">My Text</div> <button onclick="hideText()">Hide Text</button>


<script>
    function hideText() {
        document.getElementById('myText').style.display = "none";
    }
</script>

Output:

My Text

If we want to make a simple text toggler, we can approach it like in this example.

Example:

<div id="myText2">My Text</div> <button onclick="hideText2()">Hide/Show Text</button>


<script>
    function hideText2() {
        var myText = document.getElementById("myText2");
        if (myText.style.display === "none") {
            myText.style.display = "block";
        } else {
            myText.style.display = "none";
        }
    }
</script>

Output:

My Text