Add the id in the td as text

1

In that piece of code I go through the table the body and each td and to each cell I assign the color and try to assign the id of each cell (td) but all it does is repeat the last id of the last cell.

$(function () {
           $("#materiales tbody tr").each(function (index){
               $(this).children("td").each(function (index2){

                   $(".cuadro").css("background-color", "#ECF8E0");
                    $(".cuadro").text( $(this).attr("id") );


               })

           })
       }) 

Someone to help me solve it

    
asked by Soldier 04.08.2017 в 23:01
source

1 answer

1

According to what I understand, you are doing wrong the reference to the element to which you want to put the ID . In this line

$(this).children("td").each(....

You are already going through all the cells in your table. According to your comments, .cuadro is a class that all your cells have, so when you put

 $(".cuadro")...

You are referring to all the elements that have that class, so I think that the reference to the element is the one that is wrong.

What you can do is use $(this) as you are doing so far. You use it in this way to refer to the cell where you are currently:

$(this).css("background-color", "#ECF8E0");
$(this).text( $(this).attr("id") );

And so, each cell will now show the ID you've put in.

    
answered by 04.08.2017 / 23:28
source