Why are there two ways to set <td> attributes?

There are two ways to set cell attributes. Does one have any advantage over the other?

var cell = document.createElement("td");
cell.style.width = "50px";


cell.setAttribute("width", "50px");


These ways are different.
First one adds inline CSS style:

cell.style.width = "50px";
<td style="width:50px">

while second one adds attribute to the tag:

cell.setAttribute("width", "50");
<td width="50">

That means if you want to change style of the element you use first one. But when you need an attribute (for example, custom data-something="value") you have to use second.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.