Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Opening and closing dropdown in html

When I click on buttons nothing is happening. I am trying to make them open and close. How can I solve this?werrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

HTML

    <div class="sidenav">
    <img src="assets/images/placeholder-logo.svg">

    <li ng-repeat="item in data" ng-if="dataLoaded">
        <button class="dropdown-btn">{{item.streamName}}
            <i class="fa fa-caret-down"></i>
        </button>
        <div class="dropdown-container">
            <ul>
                <li ng-repeat="element in item.apps">
                    <button class="dropdown-btn">{{element.appName}}
                        <i class="fa fa-caret-down" ng-if="element.sheets.length"></i>
                    </button>
                    <ul>
                        <li ng-repeat="sheet in element.sheets">
                            <button class="dropdown-btn">{{sheet.sheetName}}
                            </button>
                        </li>
                    </ul>
                </li>
            </ul>
        </div>
    </li>
</div>

JAVASCRIPT

var dropdown = document.getElementsByClassName("dropdown-btn");
            var i;

            for (i = 0; i < dropdown.length; i++) {
            dropdown[i].addEventListener("click", function() {
                this.classList.toggle("active");
                var dropdownContent = this.nextElementSibling;
                if (dropdownContent.style.display === "block") {
                dropdownContent.style.display = "none";
                } else {
                dropdownContent.style.display = "block";
                }
            });
            } 
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Your code is working, but it will hide the content area only after the second click on the button because the initial value dropdownContent.style.display is empty string, but you check if it's equals to "block".

So the first click on the button set the content area to display: block.

You need to change the check to dropdownContent.style.display === ""

var dropdown = document.getElementsByClassName("dropdown-btn");
var i;

for (i = 0; i < dropdown.length; i++) {
    dropdown[i].addEventListener("click", function() {
        this.classList.toggle("active");
        var dropdownContent = this.nextElementSibling;
        if (dropdownContent.style.display === "block" || dropdownContent.style.display === "") {
            dropdownContent.style.display = "none";
        } else {
            dropdownContent.style.display = "block";
        }
    });
} 
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!