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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Looping through all form fields hangs the system

    Hi all,

    I'm currently working on a PHP project, in which i have a search form for users to set search criteria and displays the result. for each result row i have created a checkbox for the user to select that row such that they will be able to identify which rows to export out.

    now, as this could be a potentially generate a huge list of results, individually selecting each checkbox can be a pain. so i created a "toggle" checkbox which will toggle the checkboxes "on" or "off" using a javascript function.

    the HTML code for the "toggle"

    Code:
    <INPUT TYPE='checkbox' NAME='AutoCheck' VALUE='Autocheck-uncheck All Boxes' onClick='modify_boxes()'>
    and the code for each of the individual checkboxes

    Code:
    <input type='checkbox' name='expID[]' value='" . $info['ID'] . "'>
    the JS function

    Code:
    function modify_boxes(){
    	for ( i=0 ; i <theForm.elements.length ; i++ ){
    		if (theForm.elements[i].name=='expID[]'){
    			if (theForm.AutoCheck.checked == true) {
    				theForm.elements[i].checked = true;
    			}else{
    			 theForm.elements[i].checked = false;
    			}
    		}
    	}  
    }
    now, as this could be a potentially generate a huge list of results, looping through all the form elements looping through all the elements in the form seems inefficient (i have many other elements besides this checkbox), is there another way to achieve the same result (i.e. autocheck/uncheck) in a more effective manner?

    Thanks in advance!

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,535
    Thanks
    3
    Thanked 513 Times in 500 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script  type="text/javascript">
    /*<![CDATA[*/
    function modify_boxes(obj){
        var cbs=document.getElementsByName('expID[]');
    	for (var i=0 ; i <cbs.length; i++ ){
    			cbs[i].checked=obj.checked;
    	}
    }
    /*]]>*/
    </script></head>
    
    <body>
    <INPUT TYPE='checkbox' NAME='AutoCheck' VALUE='Autocheck-uncheck All Boxes' onClick='modify_boxes(this)'>
    <input type='checkbox' name='expID[]' value=''>
    
    </body>
    
    </html>
    Last edited by vwphillips; 07-20-2009 at 12:09 PM. Reason: oops
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    victaru (07-20-2009)

  • #3
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    good lord, thanks!

    although it still took a while, but it manages to check all 2000 test records i had

    Thanks!

  • #4
    Banned
    Join Date
    Nov 2008
    Location
    not found
    Posts
    284
    Thanks
    0
    Thanked 53 Times in 51 Posts
    Quote Originally Posted by victaru View Post
    although it still took a while, but it manages to check all 2000 test records i had
    This should make it slightly faster:
    Code:
    function modify_boxes(obj){
        var cbs=document.getElementsByName('expID[]');
    	for (var i=0, len=cbs.length ; i < len; i++ ){
    			cbs[i].checked=obj.checked;
    	}
    }

  • #5
    New Coder
    Join Date
    Mar 2009
    Location
    Fabric Covered Box
    Posts
    69
    Thanks
    1
    Thanked 16 Times in 14 Posts
    And a wee bit faster still
    Code:
    function modify_boxes(obj){
        var cbs=document.getElementsByName('expID[]'), state=obj.checked;
    	for (var i=0, len=cbs.length ; i < len; i++ ){
    			cbs[i].checked=state;
    	}
    }


  •  

    Posting Permissions

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