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

120
Vistas
Jquery coloring table cells according to condition and values

Trying to colour table rows that contain years<= 2004 in the second column and the third column should have value "Ne". What I am getting now is colored rows that contain only values in the third column "Ne" and condition years<=2005 is ignored. Is there some way to write condition using &&, or my syntax is bad?

$(function(){
        $("#randa").click(function(){
          $("#lentele td:nth-child(2)").each(function()
          {
            if (parseInt($(this).text()) <=2005)
            {
              
              $("#lentele td:nth-child(3)").each(function()
              {
                if ($(this).text() == "Ne")
                {
                  $(this).parent("tr").addClass('spalvinti');
                  
                }

                
              }
              
            )
            
            }
          })
        })

});

enter image description here

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

0

When you find applicable cell with text Ne, it needs to be in the same row as the matching cell with the year. The code with problem is finding all cells with Ne instead.

I would process row by row as below:

$("#randa").click(function(){
  $("#lentele tr").each(function() {
    if (parseInt($(this).children('td:nth-child(2)').text()) <= 2005
        && $(this).children('td:nth-child(3)').text() === 'Ne') {
      $(this).addClass('spalvinti');
    }
  });
});
table { border: 1px solid #eee; }
tr.spalvinti { background-color: green; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="randa">
  <table id="lentele">
    <tbody>
      <tr>
        <td>1</td><td>2010</td><td>Taip</td>
      </tr>
      <tr>
        <td>2</td><td>1999</td><td>Ne</td>
      </tr>
      <tr>
        <td>3</td><td>2001</td><td>Ne</td>
      </tr>
      <tr>
        <td>4</td><td>2004</td><td>Ne</td>
      </tr>
      <tr>
        <td>5</td><td>2008</td><td>Ne</td>
      </tr>
    </tbody>
  </table>
</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