Fizik26
Fizik26

Reputation: 839

How to add data-column with insertCell()?

This is my problem:
I have a table in html with parameters 'data-column' in my <td> elements like this:

<td class="column100 column1" data-column="column1">Total</td>
<td class="column100 column2" data-column="column2">--</td>

With a Javascript function, I want to add new row and columns, It's work, but the element 'data-column="columnX"' is not added and I don't know how to add it?
This is a part of my Javascript function.

var a = x.insertCell(0);
var b = x.insertCell(1);
a.className = 'column100 column1';
b.className = 'column100 column2';

The result is:

<td class="column100 column1">Mois 0</td>
<td class="column100 column2">--</td>

But I need to add the 'data-column'.

Upvotes: 2

Views: 263

Answers (1)

Peter van der Wal
Peter van der Wal

Reputation: 11806

Use setAttribute

var x = document.getElementById("x");
var a = x.insertCell(0);
a.className = 'column100 column1';
a.setAttribute("data-column", "column1");
a.innerText = x.innerHTML;
<table><tr id="x"></tr></table>

Upvotes: 3

Related Questions