Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

284
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda