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 11 of 11
  1. #1
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts

    Check All/Uncheck All checkboxes

    OK so it sounds fairly simple I have a form with many checkboxes in it all with the same name/id they're populated by php so ignore the \

    <input type=\"checkbox\" value=\"".$leads[0]."\" name=\"assignlead\" onclick=\"refreshMassUpdate(this)\">

    Now at current when a checkbox is clicked it updates a hidden field with the value of the checkbox as I pass the created hidden field to the process page for stuff to happen with it. (its a list of id's basically)

    I've noticed recently the list is getting kinda long 800+ items so rather than clicking each one individually 800 clicks upwards I want to put check all uncheck all buttons

    Quick search around google turned up some simple code to do it

    Code:
    function checkAll(field)
    {
    for (i = 0; i < field.length; i++)
    	field[i].checked = true ;
    }
    
    function uncheckAll(field)
    {
    for (i = 0; i < field.length; i++)
    	field[i].checked = false ;
    }
    which works great BUT because each checkbox is no longer being clicked its not calling the refreshMassUpdate(this) script and so not populating my hidden field.

    What I've been trying to work out is how to make the onclick of the button also clal the refreshMassUpdate(this) script so it also populates my hidden field. So far I'm an epic failure though javascript really isn't my thing. Are there's any easy ways of doing this or am I missing an obvious?
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Use this script:
    Code:
    function checkAll(e){
    for(var i=0,check=document.getElementsByTagName('input');i<check.length;i++){
    if(check[i].type=='checkbox'){
    check[i].checked=e.checked;refreshMassUpddate(check[i]);}}}
    And call the function on the "check all" checkbox:
    Code:
    <label for="lab">Check all </lab><input type="checkbox" onclick="checkAll(this)" id="lab">
    Hope it helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    doesn't work or at least certainly not they way i envisage it working.

    Hard to describe but it allows you to check each box individually but at this point doesn't update the hidden field like it should however if you click a checked box a 2nd time then it starts to randomly check unchecked boxes and clicking a 3rd time unchecks everything.

    All the time not updating the hidden field. Thats why I was after a check all/uncheck all button.

    There's actually an example of what I'm after on these here very forums If you've got a few private messages in your inbox if you look there's a series of check boxes which can be checked individually or what not and also a check all and uncheck all button at the bottom of the list. I've looked over the vb stuff though and I have to say it confuses the hell out of me i may have well been reading russian or something.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Could you please show us your markup (HTML).
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    The current refresh mass update function is like this

    Code:
    function refreshMassUpdate(checkBox) {
    	var massList = document.getElementById("List2Update");
    	var isChecked = checkBox.checked;
    	if ( massList ) {
    		var allChecked = massList.value.split(",");
    		if ( allChecked ) {
    			var newValue = "";
    			var isFound = false;
    			for ( var i = 0; i < allChecked.length; i++ ) {
    				if ( allChecked[i] == checkBox.value ) {
    					isFound = true;
    					if ( !isChecked ) {
    						continue; // remove it
    					}
    				}
    				newValue += (i > 0 && newValue != "") ? "," : "";
    				newValue += allChecked[i];
    			} // endfor i
    			if ( isChecked && !isFound ) {
    				newValue += ((newValue != "") ? "," : "") + checkBox.value;
    			}
    			massList.value = newValue;
    		} else if ( isChecked ) {
    			massList.value = checkBox.value;
    		}
    	} // endif massList
    }
    I've removed all the table formatting and other information you dont need from the block below so it just shows the checkboxes the hidden field needed to update etc


    Code:
    <form action="dashboard.php?LoadModule=office&sub=unassigned&action=assign" method="POST" id="UpdateForm" name="UpdateForm">
    <input type="hidden" id="List2Update" name="List2Update" value="" />
    <select name="AssignTo" style="width: 150px;">
    <option label="JoeBlogs" value="13">JoeBlogs</option>
    </select>
    <input type="submit" value="Assign Now" name="actionbutton" class="button" />
    <input type="button" value="Select All" class="button" onClick="checkAll(this)"/> 
    <input type="button" value="Deselect All" class="button" onClick="checkAll(this)"/>
    
    <input type="checkbox" value="2846" name="assignlead" onclick="refreshMassUpdate(this)">
    <input type="checkbox" value="2856" name="assignlead" onclick="refreshMassUpdate(this)">
    <input type="checkbox" value="3221" name="assignlead" onclick="refreshMassUpdate(this)">
    <input type="checkbox" value="3231" name="assignlead" onclick="refreshMassUpdate(this)">
    </form>
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    See if having this script helps:
    Code:
    function checkAll(e){
    if(e.value.toLowerCase()=='select all'){
    for(var i=0,assgn=document.getElementsByTagName('input');i<assgn.length;i++){
    if(assgn[i].type=='checkbox'&&assgn[i].name=='assignlead'){
    assgn[i].checked=true;refreshMassUpdate(assgn[i]);
    }}}
    else{
    for(var i=0,assgn=document.getElementsByTagName('input');i<assgn.length;i++){
    if(assgn[i].type=='checkbox'&&assgn[i].name=='assignlead'){
    assgn[i].checked=false;refreshMassUpdate(assgn[i]);
    }}}}
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #7
    Banned
    Join Date
    May 2006
    Location
    England
    Posts
    664
    Thanks
    0
    Thanked 84 Times in 84 Posts
    Quote Originally Posted by hinch View Post
    Quick search around google turned up some simple code to do it

    Code:
    function checkAll(field)
    {
    for (i = 0; i < field.length; i++)
    	field[i].checked = true ;
    }
    
    function uncheckAll(field)
    {
    for (i = 0; i < field.length; i++)
    	field[i].checked = false ;
    }
    which works great BUT because each checkbox is no longer being clicked its not calling the refreshMassUpdate(this) script and so not populating my hidden field.
    Try:
    Code:
    function checkAll(field)
    {
     for (var i = 0; i < field.length; i++)
     {
      field[i].checked = true ;
      field[i].onclick();
     }
    }

  • #8
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    Getting there

    The select all and deselect all buttons now work and correctly populate the hidden field but you can no longer individually select each item (which is also needed) you now get all or nothing
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #9
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    @Arty that doesn't work at all
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #10
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    you now get all or nothing
    Are you calling the the "select all"/ "desect all" function from all the checkboxes ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #11
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    i want the select all / deselect all to function from 2 buttons independant of the checkboxes which they're doing with rangana's code and they're correctly populating the hidden field. But I also need the checkboxes to work independantly so it could be I only want to select and action 2-3 items not ALL items in which case i need to use or use an equivilent of the current mass update function I posted above which also correctly populates the hidden field.

    I just can't get both options working together i can get one or the other working rangana's code makes the buttons work dropping rangana's code and using just mine works fine for individual buttons.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com


  •  

    Posting Permissions

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