Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I make a button that toggles the visibility of an unordered list?

Here is where my button is located:

<body>
<h3 class="paragraph">Remove the duplicates in 2 Javascript arrays (found in readme), add the results to an array and output the list of distinct names in an unordered list below this paragraph when <button type="button" onclick="show" id="btnID" class="javabutton">
        this link</button> is clicked. If the operation has been completed already, notify the user that this has already been done.</h3>

And here is the unordered list I want my button to display:

<ul class="javalist" id="javalist">
            <li>Matt Johnson</li>
            <li>Bart Paden</li>
            <li>Ryan Doss</li>
            <li>Jared Malcolm</li>
            <li>Jordan Heigle</li>
            <li>Tyler Viles</li>
        </ul>
</body>

How do I make the button toggle between hiding/showing the list?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I don’t know how you made show(), but you have to use brackets in the onclick attribute like this:

function show(){
  var list  = document.getElementById("javalist");
  if(list.style.display != "none") list.style.display = "none";
  else list.style.display = "block";
}
<body>
<h3 class="paragraph">Remove the duplicates in 2 Javascript arrays (found in readme), add the results to an array and output the list of distinct names in an unordered list below this paragraph when <button type="button" onclick="show()" id="btnID" class="javabutton" >
        this link</button> is clicked. If the operation has been completed already, notify the user that this has already been done.</h3>
<ul class="javalist" id="javalist" style="display: none;">
            <li>Matt Johnson</li>
            <li>Bart Paden</li>
            <li>Ryan Doss</li>
            <li>Jared Malcolm</li>
            <li>Jordan Heigle</li>
            <li>Tyler Viles</li>
        </ul>
</body>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading