Set attribute to dynamically generated <td> tag

15,761

Solution 1

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

Solution 2

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

cell1.className = 'styleClass';
Share:
15,761
Muhammad Imran Tariq
Author by

Muhammad Imran Tariq

I am a passionate Senior Software Engineer. Majorly work in Java and BigData. I completed my masters degree in computer science and since then I developed various business application on different domains including financial systems, digitalsignage, security etc. I am also a good web developer and worked on different websites such as blogs, shopping carts. I have good understanding of programming languages and software development pros and cons.

Updated on June 04, 2022

Comments

  • Muhammad Imran Tariq
    Muhammad Imran Tariq almost 2 years

    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 tag. e.g. <td class="styleClass">