www.webdeveloper.com
Results 1 to 7 of 7

Thread: Works in development, not on server

  1. #1
    Join Date
    Jan 2003
    Posts
    628

    Works in development, not on server

    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?

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Is your production server available online to see?
    JavaScript: Learn | Validate | Compact

  3. #3
    Join Date
    Jan 2003
    Posts
    628
    Quote Originally Posted by bionoid View Post
    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 ...

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    Is it possible for you to post the output of the page in question?
    JavaScript: Learn | Validate | Compact

  5. #5
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Code:
     if (cell.childNodes[0].type == "checkbox")
    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!

  6. #6
    Join Date
    Jan 2003
    Posts
    628
    Thanks for your replies.

    This is from the output of the page in question: here's the javascript function.

    Code:
    function checkrecipients()
    {
        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];
    
                //if (table.rows[i].cells[0].firstChild && table.rows[i].cells[0].firstChild.type == 'checkbox' && table.rows[i].cells[0].firstChild.checked)
                if (cell.getElementsByTagName('input')[0]  && cell.getElementsByTagName('input')[0].checked)
                {
                    somechecked = true;
                    //alert(table.rows[i].cells[1].getElementsByTagName('span')[1].innerHTML);
    
                    if (checkemail(table.rows[i].cells[1].getElementsByTagName('span')[1].innerHTML) == 'wrong')
                    {
                        return false;
                    }
                    //alert('somechecked in the loop(' + i + ')' + somechecked);
                }
            }
        }
        //alert('somechecked after loop ' + somechecked);
        if (somechecked == false)
        {
            alert('You have not selected a recipient.');
            return false;
        }
        return checkmail();
    }
    Here's the html of the gridview

    HTML Code:
    <table class="Grid" cellspacing="0" rules="all" border="1" id="gvRecipients" style="border-collapse:collapse;">
    				<tr>
    					<th scope="col">
                            <input id="gvRecipients_ctl01_cbAllRecipients" type="checkbox" name="gvRecipients$ctl01$cbAllRecipients" onclick="javascript:checkall();" />
                        </th><th scope="col">&nbsp;</th>
    				</tr><tr>
    					<td colspan="2" style="Background-color:#FFEBCD;font-weight:bold;color:Navy;"><span>ABCDE Organisation</span></td>
    				</tr><tr>
    					<td>
                            <input id="gvRecipients_ctl03_cbRecipient" type="checkbox" name="gvRecipients$ctl03$cbRecipient" />
                        </td><td>
                            <span id="gvRecipients_ctl03_lblRecipient"></span>
                            <span id="gvRecipients_ctl03_lblEmailAddress" class="smallsubhead">ABC123@yahoo.co.uk</span>
                        </td>
    				</tr><tr>
    					<td colspan="2" style="Background-color:#FFEBCD;font-weight:bold;color:Navy;"><span>Society with Real Potential</span></td>
    				</tr><tr>
    					<td>
                            <input id="gvRecipients_ctl05_cbRecipient" type="checkbox" name="gvRecipients$ctl05$cbRecipient" />
                        </td><td>
                            <span id="gvRecipients_ctl05_lblRecipient"></span>
                            <span id="gvRecipients_ctl05_lblEmailAddress" class="smallsubhead">abcdef@hotmail.com</span>
                        </td>
    				</tr>
    			</table>
    I have modified the javascript function as suggested by Logic Ali to use:

    Code:
    if (cell.getElementsByTagName('input')[0]  && cell.getElementsByTagName('input')[0].checked)
    and this works. (Thank you for the suggestion)

    But the bit I don't get is why my original code:

    Code:
    //if (table.rows[i].cells[0].firstChild && table.rows[i].cells[0].firstChild.type == 'checkbox' && table.rows[i].cells[0].firstChild.checked)
    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.

    Thanks for your help. It is working now.

  7. #7
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    792
    A different approach to the problem, using the ids in the document you already have:

    Code:
    function checkrecipients()
    {
    	var
    		table, inputs, id, somechecked,
    		i, j;
    
    	table = document.getElementById('gvRecipients');
    	inputs = table.getElementsByTagName('input');
    	somechecked = false;
    
    	for (i = 0; i < inputs.length; ++i) {
    		if (inputs[i].type == 'checkbox' && (id = (j = String(inputs[i].id).match(/^(.*?)cbRecipient$/)) ? j[1] : false) && inputs[i].checked) {
    			if (checkemail(document.getElementById(id + 'lblEmailAddress').innerHTML) === 'wrong') {return false;} else {somechecked = true;}
    		}
    	}
    	if (!somechecked) {
    		alert('You have not selected a recipient.');
    		return false;
    	}
    	return checkmail();
    }
    Could have been done a little easier, depending on whether you're allowed to change the html.
    Last edited by bionoid; 08-02-2012 at 05:12 AM.
    JavaScript: Learn | Validate | Compact

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles