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

176
Vistas
Create a table collapsible with less/more button?

I´ve got a table with 50 rows, which is way too much for a website if you look the first time on a page. I want to create a button that can unfold and fold up the whole table.

My big problem is that I have no knowledge about javascript :(.

I don't want to paste here 50 rows of code... Here's a shorter example:

<table>
<tbody>
    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>
    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>
    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>
<! --it should unfold here-->

    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>
    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>
</tbody>
</table>

<button onclick="toggleText()" id="showMore">Show More
</button>

Like you see in my example I want to unfold my whole table after 3 rows. What do I have to do to achieve this?

Big thanks and have a nice weekend!

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use for this case the Javascript toggle function. Something like that:

const trs = document.getElementsByTagName("tr");     
const startWith = 3;

function showMore() {       
  for (let i = startWith; i < trs.length; i++) {
    trs[i].classList.toggle('hidden')
  }
}
.hidden {
  display:none;
}
<table id="t">
<tbody>

    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>

    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>

    <tr>
        <td>row 1</td>
        <td>row 2</td>
    </tr>

<! --it should unfold here-->

    <tr class="hidden">
        <td>row 1</td>
        <td>row 2</td>
    </tr>

    <tr class="hidden">
        <td>row 1</td>
        <td>row 2</td>
    </tr>

</tbody>
</table>

<button onclick="showMore()" id="showMore">Show More
</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

As mentioned in my comment you can simply select the excess table rows with CSS selector logic.

function excerpt(){
  var excessRows = myTable.querySelectorAll("tr:nth-child(n+4)");
  excessRows.forEach(row => row.style.display = window.getComputedStyle(row).display === "none" ? "block" : "none");
}
td { border : 1px solid red}
tr { display : block}
tr:nth-child(n+4) {display : none}
<table id = "myTable">
<tbody>

    <tr>
        <td>row 1 this</td>
        <td>row 1 that</td>
    </tr>

    <tr>
        <td>row 2 this</td>
        <td>row 2 that</td>
    </tr>

    <tr>
        <td>row 3 this</td>
        <td>row 3 that</td>
    </tr>

<! --it should unfold here-->

    <tr>
        <td>row 4 this</td>
        <td>row 4 that</td>
    </tr>

    <tr>
        <td>row 5 this</td>
        <td>row 5 that</td>
    </tr>

</tbody>
</table>

<button onclick="excerpt()" id="showMore">Show More/Less</button>

about 4 years ago · Juan Pablo Isaza Denunciar

0

try below javascript code.

<script>
      function toggleText() {
        const tableRows = document.getElementsByTagName("tr");
        // initialise i with row number from which you need to hide/show
        for (let i = 3; i < x.length; i++) {
          if (tableRows[i].style.display === "none") {
            tableRows[i].style.display = "table-row";
          } else {
            tableRows[i].style.display = "none";
          }
        }
      }
    </script>
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