I have a table that is generated using js. As far as I know, I cant add unique ids to the different cells.
My question is, is it possible to add a function that copies the content of one of the clicked elements that I could add to the cells to copy there content? or is there a way to add unique classes or ids to my cells and the enclosed divs using the js below?
I'm pretty new to js so if it's not too much trouble would you mind explaining any changes made to the code so I can better understand it?
any help is much appreciated!
tried this with no success:
$('.hex-color').on('click', function(){
element = $(this).next('div');
var selection = window.getSelection();
var range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
try {
var successful = document.execCommand('copy');
if(successful) {
$('.res').html("Coppied");
}
else
{ $('.res').html("Unable to copy!");}
} catch (err) {
$('.res').html(err);
}
});
function makeTableRowColors(colors, displayType) {
var tableRow = "<tr>";
for (var i = 0; i < colors.length; i++) {
if (displayType == "colors") {
tableRow += "<td class=\"hex-color tint-shade-box\" style=\"background-color:" + "#" + colors[i].toString(16) + "\";><div style=\"background-color:" + "#" + colors[i].toString(16) + "\";>" + colors[i].toString(16).toUpperCase() + "</div></td>";
}
}
tableRow += "</tr>";
return tableRow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<tr>
<td class="hex-color tint-shade-box" style="background-color:#f5f5f5">
<div style="background-color:#f5f5f5">F5F5F5</div>
</td>
<td class="hex-color tint-shade-box" style="background-color:#dddddd">
<div style="background-color:#dddddd">DDDDDD</div>
</td>
</tr>