Hi there
i have one question about selecting chekboxes.
i have script form that adds dynamic rows and cheks/uncheks checkboxes. by default this form has one chekboxe. this function checks/uncehks only when i entered one row at least. when i dont enter any row it doesnt checks any checkboxes. who can explain and help me why?
thanks in advance
<html>
<head>
<title> Add/Remove dynamic rows in HTML table </title>
<script language="javascript">
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "checkbox";
element1.name="qutu";
element1.id="qutu";
cell1.appendChild(element1);
var cell2 = row.insertCell(1);
cell2.innerHTML = rowCount + 1;
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
cell3.appendChild(element2);
}
function deleteRow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=0; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = table.rows[i].cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(i);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
function selectAll(chk) {
for(var i=0;i<chk.length;i++) {
chk[i].checked = true;
}
}
function unselectAll(chk) {
for(var i=0;i<chk.length;i++) {
chk[i].checked = false
}
}
</script>
</head>
<body>
<form name="az">
<table id="dataTable" width="400px" border="1">
<tr>
<td colspan = "4">
<input type="button" value="Add Row" onclick="addRow('dataTable')" />
<input type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<input type="button" value="select all" onclick="selectAll(qutu)" />
<input type="button" value="Unselect all" onclick="unselectAll(qutu)" />
</td>
</tr>
<tr>
<td><input type="checkbox" name="qutu" id="qutu"/>select all</td>
<td> 1 </td>
<td> <input type="text" /> </td>
</tr>
</table>
</form>
</body>
</html>