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

290
Visualizações
How to hide and show data fetched from database and displayed on the page, and show each row from table on button click

I' am fetching data from database with a PHP code. And what I want is to hide that data and show it on click each row on table on its own, like when click a particular row it shows and the other hide. I have a JavaScript code to hide and show that data but the problem is on click the only the first row of table that is working and all the button affect only the first row fetched. What I want is every row shows on click and the other hides.

The two row needed from table are named "name" and "info".

PHP code

<?php      
        $result = mysqli_query($db,"select * from table");
        
        while($data = mysqli_fetch_array($result)) { 
            
            echo "<button onclick='myFunction()'>";
            echo "<h3>" . $data["name"] ; "</h3>";
            echo "</button>";

            echo "<div id='hide'>";
            echo "<p>" . $data["info"] ; "</p>"; 
            echo "</div>";
            
        }
    ?>

And JavaScript code:

<script>
   function myFunction() {
   var x = document.getElementById("id");
      if (x.style.display === "none") {
          x.style.display = "block";
      } else {
            x.style.display = "none";
      }
   }
</script>

And even when I change the div to class and the first JavaScript code to another one below, the problem still with another behavior:

<script>
   function myFunction() {
    var myClasses = document.querySelectorAll('.hide'),
    i = 0,
    l = myClasses.length;

    for (i; i < l; i++) {
        myClasses[i].style.display = 'block';
    }

  }
</script>

That second JavaScript code hide and show the whole the data coming.

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