I'm attempting to develop a checkall checkbox that will automatically select all the checkboxes I've created using JavaScript.
Firstly, I gather the number of rows and columns from the user and then use JavaScript to generate a table and insert checkboxes into those cells.
Now, my goal is to implement a checkall checkbox that will check all the dynamically generated checkboxes through JavaScript.
Below is the code snippet where I'm generating the cells and checkboxes...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<script type="text/javascript">
function createTable() {
// alert("invoked")
var a, b, tableElem, rowElem, colElem, check,inc,hall,theatre,row,col;
a = document.getElementById('rows').value;
b = document.getElementById('cols').value;
hall = document.getElementById('theatreName').value;
theatre = document.getElementById('hallID').value;
document.getElementById('hdnhallName').value = hall;
document.getElementById('hdntheatreName').value = theatre;
document.getElementById("norow").value = a;
document.getElementById("nocol").value = b;
//alert(document.getElementById('hdntheatreName').value);
//alert(document.getElementById('hdnhallName').value);
inc=1;
if (a == "" || b == "") {
alert("Please enter some numeric value");
} else {
tableElem = document.createElement('table');
tableElem.style.border="1px solid black";
for (var i = 0; i < a; i++) {
rowElem = document.createElement('tr');
for (var j = 0; j < b; j++) {
colElem = document.createElement('td');
colElem.style.border="1px solid black";
colElem.appendChild(document.createTextNode(inc)); //to print cell number
rowElem.appendChild(colElem);
check = document.createElement('input');
check.type = "checkbox";
check.name = "chkSeat";
//check.value = "chk"+inc;
check.value = inc;
check.id = "chk"+inc;
//alert(check.value);
colElem.appendChild(check);
inc++;
//colElem.appendChild(lab);
}
tableElem.appendChild(rowElem);
}
document.getElementById("seat_tble2_td").appendChild(tableElem);
document.getElementById("sub_seat").disabled = false;
document.getElementById("chkall").disabled=false;
}
}
function checkall(bx){
var checkboxes = document.getElementsByName('chkSeat');
for (var i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = source.checked;
}
</script>
<h1>Create seating plan</h1>
<table id="table_seat" border="1" width="500" >
<tr>
<td>
<!-- <form action="" name="f1" onsubmit=""> -->
<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td width="40%" height="40" class="txt">Theatre Name: </td>
<td width="60%"><input type="text" name="theatreName" id="theatreName"/></td>
</tr>
<tr>
<td height="46" class="txt">Hall ID : </td>
<td><input type="text" name="hallID" id="hallID"/></td>
</tr>
<tr>
<td height="46" class="txt">Rows : </td>
<td><input type="text" name="rows" id="rows"/></td>
</tr>
<tr>
<td height="46" class="txt">Columns : </td>
<td><input type="text" name="cols" id="cols"/></td>
</tr>
<tr>
<td align="right"> <input type="submit" name="Submit" value="Generate seat plan" onclick="createTable();"/></td>
<!--<td align="right"> <button style="width:20px; height:10px;" onclick="createTable();"></button></td>-->
<td valign="top" align="left"><input type="reset" name="Submit2" value=" Reset " /></td>
</tr>
</table>
<!-- </form> -->
</td>
</tr>
</table>
<form action="SeatMap" method="post" name="f1" onsubmit="">
<table id="seat_table2" cellpadding="10">
<tr>
<td id="seat_tble2_td">
</td>
</tr>
<tr>
<td id="seat_tble2_td1">
<input type="hidden" name="hdntheatreName" id="hdntheatreName"/>
<input type="hidden" name="hdnhallName" id="hdnhallName"/>
<input type="hidden" name="norow" id="norow"/>
<input type="hidden" name="nocol" id="nocol"/>
<input type="submit" value="Submit" id="sub_seat" name="sub_seat" disabled>
</td>
</tr>
<tr>
<td>
<input id="chkall" type="checkbox" name="chkall" onClick="checkAll(this);" disabled />Check All
</td>
</tr>
</table>
</form>
</body>
The JavaScript function I tried creating for checkall doesn't seem to be functioning properly. Any assistance would be greatly appreciated. I am working with JSP and Servlets for server-side coding while utilizing JavaScript or potentially jQuery on the client side.