Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    FLC
    FLC is offline
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help me to resolve this checkbox problem

    somewhere in the body tag I have this coding

    <input type="hidden" name="hiddenName_#ContractList.ContractID#" value="#Description#" />
    <input type="checkbox" name="contracts" value="#ContractList.ContractID#" />

    the variable #ContractList.ContractID# comes from a query in coldfusion

    and I have a header checkbox that trigger checkall() function

    and the code of the function is

    function checkAll()
    {
    var contractBoxes = document.ContractForm.contracts;

    for (var i = 0; i < contractBoxes.length; ++i)
    {
    if (contractBoxes[i].checked == true)
    {
    contractBoxes[i].checked = false;
    }
    else
    {
    contractBoxes[i].checked = true;
    }
    }
    }


    the document.ContractForm.contracts; refer to a input form and contracts is a checkbox name

    my problem is,

    if there is only 1 row of record, the contractBoxes.length will become undefined and will not go into the for loop

    if I force it by doing this

    function checkAll()
    {
    var contractBoxes = document.ContractForm.contracts;

    if (contractBoxes.length == null)
    {
    contractBoxes.length = 1;
    }

    for (var i = 0; i < contractBoxes.length; ++i)
    {

    if (contractBoxes[i].checked == true)
    {
    contractBoxes[i].checked = false;
    }
    else
    {
    contractBoxes[i].checked = true;
    }
    }
    }

    it goes into the for loop but it does not toggle my checkbox

    just imagine this scenario with yahoo mailbox where there is a checkbox at the left side, you click the header checkbox, all the checkboxes will be checked,

  • #2
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>47976</title>
    	</head>
    	<body>
    		<form name="ContractForm">
    			<input type="hidden" name="hiddenName_#ContractList.ContractID#" value="#Description#">
    			<input type="checkbox" name="contracts" value="#ContractList.ContractID#">
    			<input type="button" value="check all" onclick="checkAll()">
    		</form>
    
    		<script type="text/javascript">
    			function checkAll()
    			{
    				var contractBoxes = document.ContractForm.contracts;
    				if (contractBoxes.length == undefined)
    				{
    					contractBoxes = new Array(contractBoxes);
    				}
    
    				for (var i = 0; i < contractBoxes.length; ++i)
    				{
    					contractBoxes[i].checked = (contractBoxes[i].checked) ? false : true;
    				}
    			}
    		</script>
    	</body>
    </html>

  • #3
    FLC
    FLC is offline
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up impressive

    I'm really impressed with your knowledge in javascript

    really thank you very much for solving this problem...

    it is working very well


  •  

    Posting Permissions

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