Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda