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

282
Views
JavaScript show/hide divs on button click - multiple per page

I am trying to develop a page where there would be multiple divs, each of these divs have a button of which would show a "dropdown" style div below it when clicked.

Currently I have some code which when one button is clicked, it shows all of the "dropdown" styled divs on the page instead of just the one in the same container as the button.

I would like this to be done in pure JavaScript without jquery, any help would be appreciated, thank you!

HTML

<div class="fullResultsContainer">
    <div class="resultContainer">
        <div class="resultRow">
            <!--            This has multiple divs but this is the only one relevant to the issue-->
            <div class="resultMenu">
                <button class="mobileShowActivityFeedBtn" onclick="mobileActivityLog()"> Show activity feed </button>
            </div>
        </div>
        
        <div class="mobileDropDown">
            <p> This is the content I want to show on button click</p>
        </div>
    </div>

    <div class="resultContainer">
        <div class="resultRow">
            <!--            This has multiple divs but this is the only one relevant to the issue-->
            <div class="resultMenu">
                <button class="mobileShowActivityFeedBtn" onclick="mobileActivityLog()"> Show activity feed </button>
            </div>
        </div>

        <div class="mobileDropDown">
            <p> This is the content I want to show on button click</p>
        </div>
    </div>
</div>

JS

function mobileActivityLog() {
    var btn = document.getElementsByClassName("mobileShowActivityFeedBtn");
    var activity = document.getElementsByClassName("mobileDropDown");

    for(var i=0; i<btn.length; i++) {
        for(var j=0; i<activity.length; j++) {
            if(activity[j].style.display == "none") {
                activity[j].style.display = "flex"
            } else {
                activity[j].style.display = "none"
            }
        }
    }
}

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

I think the easiest way is to send a parameter to the method and getElementById with a concatenated string with the parameter.

<div class="fullResultsContainer">
    <div class="resultContainer">
        <div class="resultRow">
            <div class="resultMenu">
                <button onclick="mobileActivityLog(1)"> Show activity feed </button>
            </div>
        </div>
        
        <div class="mobileDropDown-1">
            <p> This is the content I want to show on button click</p>
        </div>
    </div>

    <div class="resultContainer">
        <div class="resultRow">
            <div class="resultMenu">
                <button onclick="mobileActivityLog(2)"> Show activity feed </button>
            </div>
        </div>

        <div id="mobileDropDown-2">
            <p> This is the content I want to show on button click</p>
        </div>
    </div>
</div>

JS

function mobileActivityLog(index) {
    var activity = document.getElementsById("mobileDropDown-" + index);
    if(activity.style.display == "none") {
         activity.style.display = "flex"
    } else {
         activity.style.display = "none"
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

One of the best possible ways to achieve this is to pass the current context using 'call' in the HTML. Use this context to target the required result container(here 'mobileDropDown' container).

function mobileActivityLog () {
    var _this = this;
    var activity = _this.closest('.resultContainer').querySelector(".mobileDropDown");
    activity.classList.toggle('hide');
}
.hide {
  display: none;
}
<div class="fullResultsContainer">
    <div class="resultContainer">
        <div class="resultRow">
            <!--            This has multiple divs but this is the only one relevant to the issue-->
            <div class="resultMenu">
                <button class="mobileShowActivityFeedBtn" onclick="mobileActivityLog.call(this)"> Show activity feed </button>
            </div>
        </div>
        
        <div class="mobileDropDown">
            <p> This is the content I want to show on button click</p>
        </div>
    </div>

    <div class="resultContainer">
        <div class="resultRow">
            <!--            This has multiple divs but this is the only one relevant to the issue-->
            <div class="resultMenu">
                <button class="mobileShowActivityFeedBtn" onclick="mobileActivityLog.call(this)"> Show activity feed </button>
            </div>
        </div>

        <div class="mobileDropDown">
            <p> This is the content I want to show on button click</p>
        </div>
    </div>
</div>

about 4 years ago · Juan Pablo Isaza 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!