admin管理员组

文章数量:1289361

I am adding dynamic row to table through JavaScript:

var cell1 = row.insertCell(0);

var element2 = document.createElement('input');    
element2.value = "valueHere";
element2.type = "text";

cell1.appendChild(element2);

It creates new row in table:

<tr>
 <td>
  <input type="text" value="valueHere">
 </td>
</tr>

I want to add a class to <td> tag. e.g. <td class="styleClass">

I am adding dynamic row to table through JavaScript:

var cell1 = row.insertCell(0);

var element2 = document.createElement('input');    
element2.value = "valueHere";
element2.type = "text";

cell1.appendChild(element2);

It creates new row in table:

<tr>
 <td>
  <input type="text" value="valueHere">
 </td>
</tr>

I want to add a class to <td> tag. e.g. <td class="styleClass">

Share Improve this question edited Jun 14, 2022 at 13:59 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Oct 4, 2011 at 11:25 Muhammad Imran TariqMuhammad Imran Tariq 23.4k47 gold badges129 silver badges191 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You need to set the className property of the table cell (cell1):

cell1.className = 'styleClass';

To add a class to a cell/TD

var cell1 = row.insertCell(0);
    cell1.className = 'styleClass';
    //cell1.style = ... whatever you want
...
rest of your code

本文标签: javascriptSet attribute to dynamically generated lttdgt tagStack Overflow