So I have table:
<table class="checkout-list">
<thead>
<tr>
<th class="checkout-title">item</th>
<th class="checkout-title">amount</th>
<th class="checkout-title">total</th>
</tr>
</thead>
<tbody>
<tr>
<td class="checkout-info"></td>
<td class="checkout-info"></td>
<td class="checkout-info"></td>
</tr>
<tr>
<td class="checkout-info"></td>
<td class="checkout-info"></td>
<td class="checkout-info"></td>
</tr>
</tbody>
</table>
And with javascript I want to take values from thead tr th and set them to tbody tr td as attributes.
I tried this:
let title = [];
document.querySelectorAll('.checkout-title').forEach(el => {
title.push(el.innerHTML);
});
document.querySelectorAll('.checkout-info').forEach((el, index) => {
el.setAttribute('data-title', title[index]);
});
Bus as it is now I only manage to assign values to first tbody tr td child and second left with undefined it looks like this:
<tbody>
<tr>
<td class="checkout-info" data-title="item"></td>
<td class="checkout-info" data-title="amount"></td>
<td class="checkout-info" data-title="total"></td>
</tr>
<tr>
<td class="checkout-info" data-title="undefined"></td>
<td class="checkout-info" data-title="undefined"></td>
<td class="checkout-info" data-title="undefined"></td>
</tr>
</tbody>
How I should fix this undefined assignation?