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.
var somechecked = false;
var table = document.getElementById('gvRecipients');
for (i = 1; i < table.rows.length; i++)
cell = table.rows[i].cells;
//if (table.rows[i].cells.firstChild && table.rows[i].cells.firstChild.type == 'checkbox' && table.rows[i].cells.firstChild.checked)
if (cell.childNodes.type == "checkbox")
somechecked = true;
if (checkemail(table.rows[i].cells.getElementsByTagName('span').innerHTML) == 'wrong')
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.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.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.
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.