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

200
Vistas
Advanced search in HTML table with Jquery

I am trying to search html table using jquery, and from examples seen in internet i managed to get it to work, but i would like to do more advanced search.

I want search to include also next row from matched row (regardless if there is any match there), how could i achieve this ?

I have this HTML at moment:

$("#myInput").on("keyup", function () {
    var value = $(this).val().toLowerCase();
    $("tbody tr").filter(function () {
        $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
});
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <p>
        <input id="myInput" type="text" placeholder="Search..">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </p>

    <table>
        <thead>
            <tr>
                <th style='text-align:left;'>Item</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Porsche</td>
            </tr>
            <tr>
                <td>Joe</td>
            </tr>
            <tr>
                <td>Ferrari</td>
            </tr>
            <tr>
                <td>John</td>
            </tr>
            <tr>
                <td>Lada</td>
            </tr>
            <tr>
                <td>Vladimir</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

So goal would be to always include +1 row to matched row ,for example if i would search Ferrari, i would like it to show Ferrari row and John row.

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

0

 $("#myInput").on("keyup", function () {
    var value = $(this).val().toLowerCase();
    $("tbody tr").filter(function () {
        if ($(this).text().toLowerCase().indexOf(value) > -1) {
            $(this).addClass("filteredNode");
        }
        else {
            $(this).removeClass("filteredNode");
        }
        $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
});
.filteredNode + tr {
    display: table-row !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>

    <style>
      
    </style>
</head>
<body>
    <p>
        <input id="myInput" type="text" placeholder="Search..">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </p>

    <table>
        <thead>
            <tr>
                <th style='text-align:left;'>Item</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Porsche</td>
            </tr>
            <tr>
                <td>Joe</td>
            </tr>
            <tr>
                <td>Ferrari</td>
            </tr>
            <tr>
                <td>John</td>
            </tr>
            <tr>
                <td>Lada</td>
            </tr>
            <tr>
                <td>Vladimir</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

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