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

106
Visualizações
Merge $var positions in a loop with PHP

I have this PHP that is helping me populate a script:

<script>
(function($){

    <?php foreach($buttons as $button) { // loop throught all available buttons ?>

    // by clicking button one
    $('.button-1').click( function () { 
        // display:none to all filters but filter-1
        $('.filter-2, .filter-3, .filter-4, .filter-5, .filter-6').css({display: "none"});
        // guarantee filter-1 will be visible with display: block;
        $('.filter-1').css({display: "block"});
    });        
            
    <?php } ?>

})(jQuery);
</script>

But the numbers of buttons and filters (button-1, filter-2, and etc) are currently manually set, and I don't know how to build the logic, where

  • First iteration? Set 'button-1', set display:hide to all filters but 'filter-1' and display:block only to 'filter-1'
  • Second iteration? Set 'button-2', set display:hide to all filters but 'filter-2' and display:block only to 'filter-2'
  • And so on until foreach is over

How can I build this logic?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Don't do it in a loop. Give all the buttons a common class button, and add a handler to all of them. This can then get the specific button number from an attribute, and use that in the code to determine which filter to display.

So the HTML for the buttons will be something like this:

<button class="button" data-filter="1">Filter 1</button>
<button class="button" data-filter="2">Filter 2</button>
and so on

and the HTML for the filters will be:

<div class="filter-1 filter">blah</div>
<div class="filter-2 filter">lorum ipsum</div>
and so on

Then your jQuery code will simply be

$(".button").click(function() {
  let filternum = $(this).data("filter");
  $(".filter").hide();
  $(`.filter-${filternum}`).show();
}

This adds the handler to all the buttons at once.

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