13
readers helped!

This helped me

How to Toggle HTML Display With JavaScript

Here's a quick JavaScript trick to control display settings. All we do is set the display of an element to none, and then use a JavaScript function to toggle the display when a user clicks something else.

Steps

  1. How.com.vn English: Step 1 Wrap the content you want to toggle display within a container.
    <div id="content" style="display:block;">This is content.</div>
  2. How.com.vn English: Step 2 Insert JavaScript code to change the display.
    <script type="text/javascript">function toggleContent() {  // Get the DOM reference  var contentId = document.getElementById("content");  // Toggle   contentId.style.display == "block" ? contentId.style.display = "none" : contentId.style.display = "block"; }</script>
    Advertisement
  3. How.com.vn English: Step 3 Use an event handler to trigger the function.
    <button onclick="toggleContent()">Toggle</button>
    Advertisement

Community Q&A

Search
Add New Question
  • Question
    How do I create CSS animation in JavaScript?
    How.com.vn English: A_random_reader
    A_random_reader
    Community Answer
    You might want to use jQuery animations. More information and documentation is available at api.jquery.com.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit

      Advertisement

      About this article

      Tested by:
      How.com.vn Technology Team
      How.com.vn is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, 23 people, some anonymous, worked to edit and improve it over time. This article has been viewed 182,533 times.
      How helpful is this?
      Co-authors: 23
      Updated: December 22, 2023
      Views: 182,533
      Thanks to all authors for creating a page that has been read 182,533 times.

      Is this article up to date?

      ⚠️ Disclaimer:

      Content from Wiki How English language website. Text is available under the Creative Commons Attribution-Share Alike License; additional terms may apply.
      Wiki How does not encourage the violation of any laws, and cannot be responsible for any violations of such laws, should you link to this domain, or use, reproduce, or republish the information contained herein.

      Notices:
      • - A few of these subjects are frequently censored by educational, governmental, corporate, parental and other filtering schemes.
      • - Some articles may contain names, images, artworks or descriptions of events that some cultures restrict access to
      • - Please note: Wiki How does not give you opinion about the law, or advice about medical. If you need specific advice (for example, medical, legal, financial or risk management), please seek a professional who is licensed or knowledgeable in that area.
      • - Readers should not judge the importance of topics based on their coverage on Wiki How, nor think a topic is important just because it is the subject of a Wiki article.

      Advertisement