I have a table with a variety of dynamically generated background colors. I'd like to use jQuery to populate the table cell contents with the cell's actual background color. I can use $("td").text("new contents");
to change the contents of the cells. I tried $("td").text($(this).css("backgroundColor"));
to put the background color into the cell, but the background color doesn't come through.
$("td").text(
$(this).css("backgroundColor")
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>lighten</td>
<td style="background-color: #d379a6;">10%</td>
<td style="background-color: white;">50%</td>
<td style="background-color: white;">100%</td>
</tr>
<tr>
<td>darken</td>
<td style="background-color: #ad3972;">10%</td>
<td style="background-color: #14060d;">50%</td>
<td style="background-color: black;">100%</td>
</tr>
<tr>
<td>mix</td>
<td style="background-color: #b24a7e;">10%</td>
<td style="background-color: #632946;">50%</td>
<td style="background-color: black;">100%</td>
</tr>
</table>