I am trying to add rows dynamically in my HTML table and its working fine. Now what I am trying to do is, I want to make them non-editable. That is, once they are created, they should behave as normal <tr> <td> elements! I tried assigning the readonly property after appending but it didn't work.
The HTML
<!DOCTYPE html>
<html>
<head>
<script src = "myScripts.js" type = "text/javascript"></script>
</head>
<body>
<table id="myTable" border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
</table><br>
<button onclick="addRow()">Add Row</button>
</body>
</html>
the Javascript:
var index = 1;
function addRow(){
var table=document.getElementById("myTable");
var row=table.insertRow(table.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "txtName"+index;
cell1.appendChild(t1);
var cell2=row.insertCell(1);
var t2=document.createElement("input");
t2.id = "txtAge"+index;
cell2.appendChild(t2);
//t2.readonly = "readonly";
index++;
}