I have a table (asp.net gridview) with checkboxes in the first cell of most of the rows. When I click a button I run a function to check that at least one of the checkboxes is checked. An extract of the function is below. It runs perfectly in development. Publish to a server, run the page and click the button and the code just ignores the checkboxes.
Code:
var somechecked = false;
if (document.getElementById('gvRecipients'))
{
var table = document.getElementById('gvRecipients');
for (i = 1; i < table.rows.length; i++)
{
cell = table.rows[i].cells[0];
alert(cell.childNodes[0].type);
//if (table.rows[i].cells[0].firstChild && table.rows[i].cells[0].firstChild.type == 'checkbox' && table.rows[i].cells[0].firstChild.checked)
if (cell.childNodes[0].type == "checkbox")
{
if (cell.childNodes[0].checked)
{
somechecked = true;
if (checkemail(table.rows[i].cells[1].getElementsByTagName('span')[1].innerHTML) == 'wrong')
{
return false;
}
alert('somechecked in the loop(' + i + ')' + somechecked);
}
}
}
}
This particular gridview has a row at the top with a checkall checkbox - so I am ignoring this - so I am looping through the table from 1 not 0.
The second row contains headings and has no checkbox in it.
This code - alert(cell.childNodes[0].type);
in the loop returns 'undefined' for the first row - which is what I would expect as there are no childNodes in the cell.
In the second row, on the development box, it returns 'checkbox' and the code that checks if the checkbox is ticked runs and sets the variable somechecked to true if it is checked.
But, when the site is published, the code - alert(cell.childNodes[0].type); returns undefined for every row.
If I have a gridview with 30 rows in and every one of them has a checkbox in the first cell (and nothing else) it still returns 'undefined'.
Works on the development box. Does not work when the site is on a server. What is going on?
Please help, it's driving me nuts and I am about to give up and simply validate on the server. How can this be happening?
Is your production server available online to see?
Unfortunately not - it's an intranet.
But the code in question is on a page that sends emails and the checkboxes are in the first cell of a gridview. You tick the box if you want the recipient to get the mail. The code is in use on at least a dozen pages in about 8 different apps but, just lately, it is not working in a couple of the apps.
It's javascript, it shouldn't be anything to do with the server or the hosting environment? I use the same browser on my production box to view my development code and the sites on the server.
What could be wrong? I've written alerts on virtually every line and, on the server, the javascript just does not 'see' the checkbox - it thinks that table.row[i].cells[0].firstChild
or table.row[i].cells[0].childNode[0]
is undefined. View source and the checkboxes are there, the ids of everything is the same ...
Your code is probably being reformatted when served from the emote server, and the first child of an element is not necessarily an HTML element. It could be and probably is a text node consisting of a newline.
You have to scan the childNodes for nodeType == 1 or nodeName == 'INPUT' or use cell.getElementsByTagName( 'input' )[ 0 ].
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
worked on my development machine but not on a server. And why it works in about 6 other applications but decided to stop working recently in 2 others. As you can see from the html - there is nothing in the first cell apart from a checkbox.
Bookmarks