How to Set Visibility of HTML Title Using JavaScript?

Estimated read time 1 min read

You can use JavaScript to set the visibility of an HTML title by modifying the style attribute of the element. Here’s an example that toggles the visibility of a title with an id of “title”:

<h1 id="title">My Title</h1>

<button onclick="toggleVisibility()">Toggle Visibility</button>

<script>
  const title = document.getElementById("title");
  let isVisible = true;
  
  function toggleVisibility() {
    if (isVisible) {
      title.style.display = "none";
    } else {
      title.style.display = "block";
    }
    isVisible = !isVisible;
  }
</script>

In the example above, the toggleVisibility() function uses JavaScript to access the h1 element with an id of “title”. The function then toggles the visibility of the title by modifying its style.display property between “block” (visible) and “none” (hidden). The function also updates the isVisible flag to keep track of the current visibility state. When the “Toggle Visibility” button is clicked, the function is triggered and changes the visibility of the title.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply