Edit full column jquery

0
<table id="tabla_autos">
 <thead>
    <tr>
        <th>Patente</th>
        <th>Color</th>
        <th>Stock</th>
    </tr>
 </thead>
<tbody>
    <tr id="1">
        <td>LJ5213</td>
        <td id="id_color">Azul</td>
        <td>10</td>
    </tr>

    <tr id="2">
        <td>JK5123</td>
        <td id="id_color" >Amarillo</td><!-- color a reemplazar-->
        <td>15</td>
    </tr>

    <tr id="3">
        <td>YH512</td>
        <td id="id_color">cafe</td>
        <td>23</td>
    </tr>
    <tr id="3">
        <td>FD6521</td>
        <td id="id_color">Amarillo</td><!-- color a reemplazar-->
        <td>56</td>
    </tr>



</tbody>
</table>

I need to change the Color column when it is yellow and replace it with orange, this is what I have:

$("#tabla_autos").find("tbody").find("td").each(function(){ 
   if ($(this).attr("id") == "id_color") {
   $("#id_color").text("Naranjo");
  }
}); 
    
asked by Javier Antonio Aguayo Aguilar 06.06.2017 в 18:20
source

1 answer

1

There are several problems there !, starting with two <tr> with the same id and that is incorrect, plus all <td> have the same id that is id_color . I recommend that instead of id you use a class. Followed by this, check the text to know which one to change. In this case you want to change all those with Yellow and this you can do if ($(this).text() == 'Amarillo')

And to change the value you can use this:

$("#tabla_autos").find("tbody").find("td").each(function(){ 
    if ($(this).attr("class") == "id_color") {
       if ($(this).text() == 'Amarillo') {
           $(this).text("Naranjo");
       }
    }
}); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tabla_autos">
 <thead>
    <tr>
        <th>Patente</th>
        <th>Color</th>
        <th>Stock</th>
    </tr>
 </thead>
<tbody>
    <tr id="1">
        <td>LJ5213</td>
        <td class="id_color">Azul</td>
        <td>10</td>
    </tr>

    <tr id="2">
        <td>JK5123</td>
        <td class="id_color" >Amarillo</td><!-- color a reemplazar-->
        <td>15</td>
    </tr>

    <tr id="3">
        <td>YH512</td>
        <td class="id_color">cafe</td>
        <td>23</td>
    </tr>
    <tr id="3">
        <td>FD6521</td>
        <td class="id_color">Amarillo</td><!-- color a reemplazar-->
        <td>56</td>
    </tr>



</tbody>
</table>
    
answered by 06.06.2017 / 18:25
source