Alright, so there is tons of documentation out there that I have filed through and attempted many times throughout the day and can not seem to nail this issue down. I have multiple Gridviews on one page (inside of collapsible panels), of which two have a column (the 7th column, from 0) that contains checkboxes. I want to enable the user to select/deselect all of the checkboxes using a checkbox field in the header row. I prefer to accomplish this using Javascript, but can't seem to be able to get there. This is the way I prefer to go since it seems that it would work with multiple tables on the page (correct me if I am wrong). Using Firebug, there were no errors, it just simply isn't working and I can't find out why.
Here is my ASP.NET code:
<asp:GridView ID="gvSerialNumberDetails" CellPadding="5" runat="server" CssClass="wind" AutoGenerateColumns="false">
<HeaderStyle CssClass="windHeader" />
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:BoundField DataField="Serial Number" HeaderText="Serial Number" />
<asp:BoundField DataField="Facility" HeaderText="Facility" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="EmpID" HeaderText="EmpID" />
<asp:BoundField DataField="Configuration" HeaderText="Config" />
<asp:BoundField DataField="Error" HeaderText="Errors" />
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkHeader" ToolTip="Select All" runat="server"
onclick="changeAllCheckBoxes(this)" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkItem" runat="server" ToolTip="Select this item" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is my Javascript:
<script type="text/javascript" language="javascript">
function changeAllCheckBoxes(sender) {
var gridViewRows = GetParentElementByTagName(sender, "table").rows;
for (var i = 1; i < gridViewRows.length; ++i) {
gridViewRows[i].cells[7].childNodes[0].checked = sender.checked;
}
}
function GetParentElementByTagName(element, tagName) {
var element = element;
while (element.tagName != tagName)
element = element.parentNode;
return element;
}
</script>
UPDATE
Alright, so I AM getting an error now. "TypeError: element is null [Break On This Error] </tr><tr>" How do I go about fixing this? It seems like it should climb up the DOM and find the table element, but doesn't.